├── .gitignore ├── .gitmodules ├── LICENSE └── MetalTest ├── MetalTest.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcshareddata │ └── xcschemes │ └── MetalTest.xcscheme └── MetalTest ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Info.plist ├── ViewController.h ├── ViewController.m ├── main.m ├── resources └── avatar.JPG └── src ├── tests ├── 1. 三角形 │ ├── Triangle.metal │ ├── TriangleViewController.h │ └── TriangleViewController.m ├── 10. GPGPU通用计算(Compute Shader) │ ├── ComputeShader.metal │ ├── ComputeShaderViewController.h │ └── ComputeShaderViewController.m ├── 11.混合 │ ├── Blending.metal │ ├── BlendingViewController.h │ └── BlendingViewController.m ├── 2. 画一张图片 │ ├── DrawImage.metal │ ├── DrawImageViewController.h │ └── DrawImageViewController.m ├── 3. 纹理采样参数详解 │ ├── TextureSampling.metal │ ├── TextureSamplingViewController.h │ ├── TextureSamplingViewController.m │ └── TextureSamplingViewController.xib ├── 4. Metal 滤镜链 │ ├── FilterChain.metal │ ├── FilterChainViewController.h │ ├── FilterChainViewController.m │ └── FilterChainViewController.xib ├── 5. 三维变换 │ ├── ThreeDimentionsTransform.metal │ ├── ThreeDimentionsTransformViewController.h │ ├── ThreeDimentionsTransformViewController.m │ └── ThreeDimentionsTransformViewController.xib ├── 6. 旋转的立方体 │ ├── RotatingCube.metal │ ├── RotatingCubeViewController.h │ ├── RotatingCubeViewController.m │ └── RotatingCubeViewController.xib ├── 7. 渲染摄像头采集的数据(CVMetalTextureCache) │ ├── RenderCameraBGRA.metal │ ├── RenderCameraBGRAViewController.h │ └── RenderCameraBGRAViewController.m ├── 8. 渲染摄像头采集的 YUV 数据 │ ├── RenderCameraYUV.metal │ ├── RenderCameraYUVViewController.h │ └── RenderCameraYUVViewController.m └── 9. 光照(冯氏光照模型) │ ├── PhongLight.metal │ ├── PhongLightViewController.h │ └── PhongLightViewController.m └── utils ├── MetalUtils.h ├── MetalUtils.m └── YUV_To_RGB_Matrices_Vectors.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | # CocoaPods 34 | # 35 | # We recommend against adding the Pods directory to your .gitignore. However 36 | # you should judge for yourself, the pros and cons are mentioned at: 37 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 38 | # 39 | # Pods/ 40 | # 41 | # Add this line if you want to avoid checking in source code from the Xcode workspace 42 | # *.xcworkspace 43 | 44 | # Carthage 45 | # 46 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 47 | # Carthage/Checkouts 48 | 49 | Carthage/Build/ 50 | 51 | # fastlane 52 | # 53 | # It is recommended to not store the screenshots in the git repo. 54 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 55 | # For more information about the recommended setup visit: 56 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 57 | 58 | fastlane/report.xml 59 | fastlane/Preview.html 60 | fastlane/screenshots/**/*.png 61 | fastlane/test_output 62 | 63 | # Code Injection 64 | # 65 | # After new code Injection tools there's a generated folder /iOSInjectionProject 66 | # https://github.com/johnno1962/injectionforxcode 67 | 68 | iOSInjectionProject/ 69 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "MetalTest/MetalTest/src/utils/MetalMatrixKit"] 2 | path = MetalTest/MetalTest/src/utils/MetalMatrixKit 3 | url = git@github.com:SourceKim/MetalMatrix.git 4 | [submodule "MetalMatrix"] 5 | path = MetalMatrix 6 | url = git@github.com:SourceKim/MetalMatrix.git 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Kim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MetalTest/MetalTest.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 3B4DB89B248E0E5E004E14ED /* ThreeDimentionsTransformViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B4DB899248E0E5E004E14ED /* ThreeDimentionsTransformViewController.m */; }; 11 | 3B4DB89C248E0E5E004E14ED /* ThreeDimentionsTransformViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3B4DB89A248E0E5E004E14ED /* ThreeDimentionsTransformViewController.xib */; }; 12 | 3B4DB89E248E0FCC004E14ED /* ThreeDimentionsTransform.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3B4DB89D248E0FCC004E14ED /* ThreeDimentionsTransform.metal */; }; 13 | 3B4DB8A1248E2400004E14ED /* MetalUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B4DB8A0248E2400004E14ED /* MetalUtils.m */; }; 14 | 3B6CB7092487A7DC0094E14A /* DrawImageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B6CB7082487A7DC0094E14A /* DrawImageViewController.m */; }; 15 | 3B6CB70C2487A8050094E14A /* avatar.JPG in Resources */ = {isa = PBXBuildFile; fileRef = 3B6CB70B2487A8050094E14A /* avatar.JPG */; }; 16 | 3B6CB70E2487ABD90094E14A /* DrawImage.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3B6CB70D2487ABD90094E14A /* DrawImage.metal */; }; 17 | 3B7348D3249624000076E133 /* Blending.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3B7348D2249624000076E133 /* Blending.metal */; }; 18 | 3B7ADA16248938D000CA5AA3 /* TextureSamplingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B7ADA12248938D000CA5AA3 /* TextureSamplingViewController.m */; }; 19 | 3B7ADA17248938D000CA5AA3 /* TextureSampling.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3B7ADA13248938D000CA5AA3 /* TextureSampling.metal */; }; 20 | 3B7ADA18248938D000CA5AA3 /* TextureSamplingViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3B7ADA14248938D000CA5AA3 /* TextureSamplingViewController.xib */; }; 21 | 3B7ADA1D2489390B00CA5AA3 /* FilterChainViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B7ADA1B2489390B00CA5AA3 /* FilterChainViewController.m */; }; 22 | 3B7ADA1E2489390B00CA5AA3 /* FilterChainViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3B7ADA1C2489390B00CA5AA3 /* FilterChainViewController.xib */; }; 23 | 3B7ADA2424893B7900CA5AA3 /* FilterChain.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3B7ADA2324893B7900CA5AA3 /* FilterChain.metal */; }; 24 | 3B7B70CD2483D06400A0D86B /* TriangleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B7B70CC2483D06400A0D86B /* TriangleViewController.m */; }; 25 | 3B7B70D02483D7A000A0D86B /* Triangle.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3B7B70CF2483D7A000A0D86B /* Triangle.metal */; }; 26 | 3B9A250F249536B100D03D27 /* BlendingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B9A250E249536B100D03D27 /* BlendingViewController.m */; }; 27 | 3BA01F1E2482CFAD002F82DF /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA01F1D2482CFAD002F82DF /* AppDelegate.m */; }; 28 | 3BA01F242482CFAD002F82DF /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA01F232482CFAD002F82DF /* ViewController.m */; }; 29 | 3BA01F292482CFB2002F82DF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3BA01F282482CFB2002F82DF /* Assets.xcassets */; }; 30 | 3BA01F2F2482CFB2002F82DF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BA01F2E2482CFB2002F82DF /* main.m */; }; 31 | 3BACD38E249231C2008C4C6D /* ComputeShaderViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BACD38D249231C2008C4C6D /* ComputeShaderViewController.m */; }; 32 | 3BACD390249232D7008C4C6D /* ComputeShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3BACD38F249232D7008C4C6D /* ComputeShader.metal */; }; 33 | 3BB42A36248F90B3003C62B7 /* RenderCameraYUVViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BB42A35248F90B3003C62B7 /* RenderCameraYUVViewController.m */; }; 34 | 3BB42A38248F90C5003C62B7 /* RenderCameraYUV.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3BB42A37248F90C5003C62B7 /* RenderCameraYUV.metal */; }; 35 | 3BB42A3D2490B7D9003C62B7 /* PhongLightViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BB42A3C2490B7D9003C62B7 /* PhongLightViewController.m */; }; 36 | 3BB42A622490DA83003C62B7 /* PhongLight.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3BB42A612490DA83003C62B7 /* PhongLight.metal */; }; 37 | 3BD530E5248E444500E11997 /* RotatingCubeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BD530E3248E444500E11997 /* RotatingCubeViewController.m */; }; 38 | 3BD530E6248E444500E11997 /* RotatingCubeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3BD530E4248E444500E11997 /* RotatingCubeViewController.xib */; }; 39 | 3BD530E8248E445300E11997 /* RotatingCube.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3BD530E7248E445300E11997 /* RotatingCube.metal */; }; 40 | 3BD530EC248F3F5B00E11997 /* RenderCameraBGRAViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BD530EB248F3F5B00E11997 /* RenderCameraBGRAViewController.m */; }; 41 | 3BD530EE248F619D00E11997 /* RenderCameraBGRA.metal in Sources */ = {isa = PBXBuildFile; fileRef = 3BD530ED248F619D00E11997 /* RenderCameraBGRA.metal */; }; 42 | 3BD530FC248F68D700E11997 /* MetalMatrix.m in Sources */ = {isa = PBXBuildFile; fileRef = 3BD530F8248F68D700E11997 /* MetalMatrix.m */; }; 43 | 3BD530FD248F68D700E11997 /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = 3BD530F9248F68D700E11997 /* LICENSE */; }; 44 | 3BD530FE248F68D700E11997 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 3BD530FA248F68D700E11997 /* README.md */; }; 45 | /* End PBXBuildFile section */ 46 | 47 | /* Begin PBXFileReference section */ 48 | 3B4DB898248E0E5E004E14ED /* ThreeDimentionsTransformViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ThreeDimentionsTransformViewController.h; sourceTree = ""; }; 49 | 3B4DB899248E0E5E004E14ED /* ThreeDimentionsTransformViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ThreeDimentionsTransformViewController.m; sourceTree = ""; }; 50 | 3B4DB89A248E0E5E004E14ED /* ThreeDimentionsTransformViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ThreeDimentionsTransformViewController.xib; sourceTree = ""; }; 51 | 3B4DB89D248E0FCC004E14ED /* ThreeDimentionsTransform.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = ThreeDimentionsTransform.metal; sourceTree = ""; }; 52 | 3B4DB89F248E2400004E14ED /* MetalUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MetalUtils.h; sourceTree = ""; }; 53 | 3B4DB8A0248E2400004E14ED /* MetalUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MetalUtils.m; sourceTree = ""; }; 54 | 3B6CB7072487A7DC0094E14A /* DrawImageViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DrawImageViewController.h; sourceTree = ""; }; 55 | 3B6CB7082487A7DC0094E14A /* DrawImageViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DrawImageViewController.m; sourceTree = ""; }; 56 | 3B6CB70B2487A8050094E14A /* avatar.JPG */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = avatar.JPG; sourceTree = ""; }; 57 | 3B6CB70D2487ABD90094E14A /* DrawImage.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = DrawImage.metal; sourceTree = ""; }; 58 | 3B7348D2249624000076E133 /* Blending.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Blending.metal; sourceTree = ""; }; 59 | 3B7ADA12248938D000CA5AA3 /* TextureSamplingViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TextureSamplingViewController.m; sourceTree = ""; }; 60 | 3B7ADA13248938D000CA5AA3 /* TextureSampling.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = TextureSampling.metal; sourceTree = ""; }; 61 | 3B7ADA14248938D000CA5AA3 /* TextureSamplingViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = TextureSamplingViewController.xib; sourceTree = ""; }; 62 | 3B7ADA15248938D000CA5AA3 /* TextureSamplingViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextureSamplingViewController.h; sourceTree = ""; }; 63 | 3B7ADA1A2489390B00CA5AA3 /* FilterChainViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FilterChainViewController.h; sourceTree = ""; }; 64 | 3B7ADA1B2489390B00CA5AA3 /* FilterChainViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FilterChainViewController.m; sourceTree = ""; }; 65 | 3B7ADA1C2489390B00CA5AA3 /* FilterChainViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = FilterChainViewController.xib; sourceTree = ""; }; 66 | 3B7ADA2324893B7900CA5AA3 /* FilterChain.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = FilterChain.metal; sourceTree = ""; }; 67 | 3B7B70CB2483D06400A0D86B /* TriangleViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TriangleViewController.h; sourceTree = ""; }; 68 | 3B7B70CC2483D06400A0D86B /* TriangleViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TriangleViewController.m; sourceTree = ""; }; 69 | 3B7B70CF2483D7A000A0D86B /* Triangle.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Triangle.metal; sourceTree = ""; }; 70 | 3B9A250D249536B100D03D27 /* BlendingViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BlendingViewController.h; sourceTree = ""; }; 71 | 3B9A250E249536B100D03D27 /* BlendingViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BlendingViewController.m; sourceTree = ""; }; 72 | 3BA01F192482CFAD002F82DF /* MetalTest.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MetalTest.app; sourceTree = BUILT_PRODUCTS_DIR; }; 73 | 3BA01F1C2482CFAD002F82DF /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 74 | 3BA01F1D2482CFAD002F82DF /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 75 | 3BA01F222482CFAD002F82DF /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 76 | 3BA01F232482CFAD002F82DF /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 77 | 3BA01F282482CFB2002F82DF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 78 | 3BA01F2D2482CFB2002F82DF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 79 | 3BA01F2E2482CFB2002F82DF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 80 | 3BACD38C249231C2008C4C6D /* ComputeShaderViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ComputeShaderViewController.h; sourceTree = ""; }; 81 | 3BACD38D249231C2008C4C6D /* ComputeShaderViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ComputeShaderViewController.m; sourceTree = ""; }; 82 | 3BACD38F249232D7008C4C6D /* ComputeShader.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = ComputeShader.metal; sourceTree = ""; }; 83 | 3BB42A34248F90B3003C62B7 /* RenderCameraYUVViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RenderCameraYUVViewController.h; sourceTree = ""; }; 84 | 3BB42A35248F90B3003C62B7 /* RenderCameraYUVViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RenderCameraYUVViewController.m; sourceTree = ""; }; 85 | 3BB42A37248F90C5003C62B7 /* RenderCameraYUV.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = RenderCameraYUV.metal; sourceTree = ""; }; 86 | 3BB42A39248F91EF003C62B7 /* YUV_To_RGB_Matrices_Vectors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = YUV_To_RGB_Matrices_Vectors.h; sourceTree = ""; }; 87 | 3BB42A3B2490B7D9003C62B7 /* PhongLightViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PhongLightViewController.h; sourceTree = ""; }; 88 | 3BB42A3C2490B7D9003C62B7 /* PhongLightViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PhongLightViewController.m; sourceTree = ""; }; 89 | 3BB42A612490DA83003C62B7 /* PhongLight.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = PhongLight.metal; sourceTree = ""; }; 90 | 3BD530E2248E444500E11997 /* RotatingCubeViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RotatingCubeViewController.h; sourceTree = ""; }; 91 | 3BD530E3248E444500E11997 /* RotatingCubeViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RotatingCubeViewController.m; sourceTree = ""; }; 92 | 3BD530E4248E444500E11997 /* RotatingCubeViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = RotatingCubeViewController.xib; sourceTree = ""; }; 93 | 3BD530E7248E445300E11997 /* RotatingCube.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = RotatingCube.metal; sourceTree = ""; }; 94 | 3BD530EA248F3F5B00E11997 /* RenderCameraBGRAViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RenderCameraBGRAViewController.h; sourceTree = ""; }; 95 | 3BD530EB248F3F5B00E11997 /* RenderCameraBGRAViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RenderCameraBGRAViewController.m; sourceTree = ""; }; 96 | 3BD530ED248F619D00E11997 /* RenderCameraBGRA.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = RenderCameraBGRA.metal; sourceTree = ""; }; 97 | 3BD530F8248F68D700E11997 /* MetalMatrix.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MetalMatrix.m; sourceTree = ""; }; 98 | 3BD530F9248F68D700E11997 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; 99 | 3BD530FA248F68D700E11997 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 100 | 3BD530FB248F68D700E11997 /* MetalMatrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MetalMatrix.h; sourceTree = ""; }; 101 | /* End PBXFileReference section */ 102 | 103 | /* Begin PBXFrameworksBuildPhase section */ 104 | 3BA01F162482CFAD002F82DF /* Frameworks */ = { 105 | isa = PBXFrameworksBuildPhase; 106 | buildActionMask = 2147483647; 107 | files = ( 108 | ); 109 | runOnlyForDeploymentPostprocessing = 0; 110 | }; 111 | /* End PBXFrameworksBuildPhase section */ 112 | 113 | /* Begin PBXGroup section */ 114 | 3B4DB897248E0E31004E14ED /* 5. 三维变换 */ = { 115 | isa = PBXGroup; 116 | children = ( 117 | 3B4DB898248E0E5E004E14ED /* ThreeDimentionsTransformViewController.h */, 118 | 3B4DB899248E0E5E004E14ED /* ThreeDimentionsTransformViewController.m */, 119 | 3B4DB89A248E0E5E004E14ED /* ThreeDimentionsTransformViewController.xib */, 120 | 3B4DB89D248E0FCC004E14ED /* ThreeDimentionsTransform.metal */, 121 | ); 122 | path = "5. 三维变换"; 123 | sourceTree = ""; 124 | }; 125 | 3B6CB7002487A4C80094E14A /* 2. 画一张图片 */ = { 126 | isa = PBXGroup; 127 | children = ( 128 | 3B6CB7072487A7DC0094E14A /* DrawImageViewController.h */, 129 | 3B6CB7082487A7DC0094E14A /* DrawImageViewController.m */, 130 | 3B6CB70D2487ABD90094E14A /* DrawImage.metal */, 131 | ); 132 | path = "2. 画一张图片"; 133 | sourceTree = ""; 134 | }; 135 | 3B6CB70A2487A7EB0094E14A /* resources */ = { 136 | isa = PBXGroup; 137 | children = ( 138 | 3B6CB70B2487A8050094E14A /* avatar.JPG */, 139 | ); 140 | path = resources; 141 | sourceTree = ""; 142 | }; 143 | 3B7ADA11248938D000CA5AA3 /* 3. 纹理采样参数详解 */ = { 144 | isa = PBXGroup; 145 | children = ( 146 | 3B7ADA13248938D000CA5AA3 /* TextureSampling.metal */, 147 | 3B7ADA15248938D000CA5AA3 /* TextureSamplingViewController.h */, 148 | 3B7ADA12248938D000CA5AA3 /* TextureSamplingViewController.m */, 149 | 3B7ADA14248938D000CA5AA3 /* TextureSamplingViewController.xib */, 150 | ); 151 | path = "3. 纹理采样参数详解"; 152 | sourceTree = ""; 153 | }; 154 | 3B7ADA19248938DF00CA5AA3 /* 4. Metal 滤镜链 */ = { 155 | isa = PBXGroup; 156 | children = ( 157 | 3B7ADA1A2489390B00CA5AA3 /* FilterChainViewController.h */, 158 | 3B7ADA1B2489390B00CA5AA3 /* FilterChainViewController.m */, 159 | 3B7ADA1C2489390B00CA5AA3 /* FilterChainViewController.xib */, 160 | 3B7ADA2324893B7900CA5AA3 /* FilterChain.metal */, 161 | ); 162 | path = "4. Metal 滤镜链"; 163 | sourceTree = ""; 164 | }; 165 | 3B7B70C72483CFD600A0D86B /* src */ = { 166 | isa = PBXGroup; 167 | children = ( 168 | 3B7B70C92483CFF100A0D86B /* utils */, 169 | 3B7B70CA2483CFF700A0D86B /* tests */, 170 | ); 171 | path = src; 172 | sourceTree = ""; 173 | }; 174 | 3B7B70C92483CFF100A0D86B /* utils */ = { 175 | isa = PBXGroup; 176 | children = ( 177 | 3BD530F7248F68D700E11997 /* MetalMatrix */, 178 | 3B4DB89F248E2400004E14ED /* MetalUtils.h */, 179 | 3B4DB8A0248E2400004E14ED /* MetalUtils.m */, 180 | 3BB42A39248F91EF003C62B7 /* YUV_To_RGB_Matrices_Vectors.h */, 181 | ); 182 | path = utils; 183 | sourceTree = ""; 184 | }; 185 | 3B7B70CA2483CFF700A0D86B /* tests */ = { 186 | isa = PBXGroup; 187 | children = ( 188 | 3B7B70CE2483D21B00A0D86B /* 1. 三角形 */, 189 | 3B6CB7002487A4C80094E14A /* 2. 画一张图片 */, 190 | 3B7ADA11248938D000CA5AA3 /* 3. 纹理采样参数详解 */, 191 | 3B7ADA19248938DF00CA5AA3 /* 4. Metal 滤镜链 */, 192 | 3B4DB897248E0E31004E14ED /* 5. 三维变换 */, 193 | 3BD530E1248E442100E11997 /* 6. 旋转的立方体 */, 194 | 3BD530E9248F3C2C00E11997 /* 7. 渲染摄像头采集的数据(CVMetalTextureCache) */, 195 | 3BB42A33248F907F003C62B7 /* 8. 渲染摄像头采集的 YUV 数据 */, 196 | 3BB42A3A2490B780003C62B7 /* 9. 光照(冯氏光照模型) */, 197 | 3BACD38B24922859008C4C6D /* 10. GPGPU通用计算(Compute Shader) */, 198 | 3B9A250C2495367500D03D27 /* 11.混合 */, 199 | ); 200 | path = tests; 201 | sourceTree = ""; 202 | }; 203 | 3B7B70CE2483D21B00A0D86B /* 1. 三角形 */ = { 204 | isa = PBXGroup; 205 | children = ( 206 | 3B7B70CB2483D06400A0D86B /* TriangleViewController.h */, 207 | 3B7B70CC2483D06400A0D86B /* TriangleViewController.m */, 208 | 3B7B70CF2483D7A000A0D86B /* Triangle.metal */, 209 | ); 210 | path = "1. 三角形"; 211 | sourceTree = ""; 212 | }; 213 | 3B9A250C2495367500D03D27 /* 11.混合 */ = { 214 | isa = PBXGroup; 215 | children = ( 216 | 3B9A250D249536B100D03D27 /* BlendingViewController.h */, 217 | 3B9A250E249536B100D03D27 /* BlendingViewController.m */, 218 | 3B7348D2249624000076E133 /* Blending.metal */, 219 | ); 220 | path = "11.混合"; 221 | sourceTree = ""; 222 | }; 223 | 3BA01F102482CFAD002F82DF = { 224 | isa = PBXGroup; 225 | children = ( 226 | 3BA01F1B2482CFAD002F82DF /* MetalTest */, 227 | 3BA01F1A2482CFAD002F82DF /* Products */, 228 | ); 229 | sourceTree = ""; 230 | }; 231 | 3BA01F1A2482CFAD002F82DF /* Products */ = { 232 | isa = PBXGroup; 233 | children = ( 234 | 3BA01F192482CFAD002F82DF /* MetalTest.app */, 235 | ); 236 | name = Products; 237 | sourceTree = ""; 238 | }; 239 | 3BA01F1B2482CFAD002F82DF /* MetalTest */ = { 240 | isa = PBXGroup; 241 | children = ( 242 | 3B6CB70A2487A7EB0094E14A /* resources */, 243 | 3B7B70C72483CFD600A0D86B /* src */, 244 | 3BA01F1C2482CFAD002F82DF /* AppDelegate.h */, 245 | 3BA01F1D2482CFAD002F82DF /* AppDelegate.m */, 246 | 3BA01F222482CFAD002F82DF /* ViewController.h */, 247 | 3BA01F232482CFAD002F82DF /* ViewController.m */, 248 | 3BA01F282482CFB2002F82DF /* Assets.xcassets */, 249 | 3BA01F2D2482CFB2002F82DF /* Info.plist */, 250 | 3BA01F2E2482CFB2002F82DF /* main.m */, 251 | ); 252 | path = MetalTest; 253 | sourceTree = ""; 254 | }; 255 | 3BACD38B24922859008C4C6D /* 10. GPGPU通用计算(Compute Shader) */ = { 256 | isa = PBXGroup; 257 | children = ( 258 | 3BACD38C249231C2008C4C6D /* ComputeShaderViewController.h */, 259 | 3BACD38D249231C2008C4C6D /* ComputeShaderViewController.m */, 260 | 3BACD38F249232D7008C4C6D /* ComputeShader.metal */, 261 | ); 262 | path = "10. GPGPU通用计算(Compute Shader)"; 263 | sourceTree = ""; 264 | }; 265 | 3BB42A33248F907F003C62B7 /* 8. 渲染摄像头采集的 YUV 数据 */ = { 266 | isa = PBXGroup; 267 | children = ( 268 | 3BB42A34248F90B3003C62B7 /* RenderCameraYUVViewController.h */, 269 | 3BB42A35248F90B3003C62B7 /* RenderCameraYUVViewController.m */, 270 | 3BB42A37248F90C5003C62B7 /* RenderCameraYUV.metal */, 271 | ); 272 | path = "8. 渲染摄像头采集的 YUV 数据"; 273 | sourceTree = ""; 274 | }; 275 | 3BB42A3A2490B780003C62B7 /* 9. 光照(冯氏光照模型) */ = { 276 | isa = PBXGroup; 277 | children = ( 278 | 3BB42A3B2490B7D9003C62B7 /* PhongLightViewController.h */, 279 | 3BB42A3C2490B7D9003C62B7 /* PhongLightViewController.m */, 280 | 3BB42A612490DA83003C62B7 /* PhongLight.metal */, 281 | ); 282 | path = "9. 光照(冯氏光照模型)"; 283 | sourceTree = ""; 284 | }; 285 | 3BD530E1248E442100E11997 /* 6. 旋转的立方体 */ = { 286 | isa = PBXGroup; 287 | children = ( 288 | 3BD530E2248E444500E11997 /* RotatingCubeViewController.h */, 289 | 3BD530E3248E444500E11997 /* RotatingCubeViewController.m */, 290 | 3BD530E4248E444500E11997 /* RotatingCubeViewController.xib */, 291 | 3BD530E7248E445300E11997 /* RotatingCube.metal */, 292 | ); 293 | path = "6. 旋转的立方体"; 294 | sourceTree = ""; 295 | }; 296 | 3BD530E9248F3C2C00E11997 /* 7. 渲染摄像头采集的数据(CVMetalTextureCache) */ = { 297 | isa = PBXGroup; 298 | children = ( 299 | 3BD530EA248F3F5B00E11997 /* RenderCameraBGRAViewController.h */, 300 | 3BD530EB248F3F5B00E11997 /* RenderCameraBGRAViewController.m */, 301 | 3BD530ED248F619D00E11997 /* RenderCameraBGRA.metal */, 302 | ); 303 | path = "7. 渲染摄像头采集的数据(CVMetalTextureCache)"; 304 | sourceTree = ""; 305 | }; 306 | 3BD530F7248F68D700E11997 /* MetalMatrix */ = { 307 | isa = PBXGroup; 308 | children = ( 309 | 3BD530F8248F68D700E11997 /* MetalMatrix.m */, 310 | 3BD530F9248F68D700E11997 /* LICENSE */, 311 | 3BD530FA248F68D700E11997 /* README.md */, 312 | 3BD530FB248F68D700E11997 /* MetalMatrix.h */, 313 | ); 314 | name = MetalMatrix; 315 | path = ../../../../MetalMatrix; 316 | sourceTree = ""; 317 | }; 318 | /* End PBXGroup section */ 319 | 320 | /* Begin PBXNativeTarget section */ 321 | 3BA01F182482CFAD002F82DF /* MetalTest */ = { 322 | isa = PBXNativeTarget; 323 | buildConfigurationList = 3BA01F322482CFB2002F82DF /* Build configuration list for PBXNativeTarget "MetalTest" */; 324 | buildPhases = ( 325 | 3BA01F152482CFAD002F82DF /* Sources */, 326 | 3BA01F162482CFAD002F82DF /* Frameworks */, 327 | 3BA01F172482CFAD002F82DF /* Resources */, 328 | ); 329 | buildRules = ( 330 | ); 331 | dependencies = ( 332 | ); 333 | name = MetalTest; 334 | productName = MetalTest; 335 | productReference = 3BA01F192482CFAD002F82DF /* MetalTest.app */; 336 | productType = "com.apple.product-type.application"; 337 | }; 338 | /* End PBXNativeTarget section */ 339 | 340 | /* Begin PBXProject section */ 341 | 3BA01F112482CFAD002F82DF /* Project object */ = { 342 | isa = PBXProject; 343 | attributes = { 344 | LastUpgradeCheck = 1150; 345 | ORGANIZATIONNAME = "苏金劲"; 346 | TargetAttributes = { 347 | 3BA01F182482CFAD002F82DF = { 348 | CreatedOnToolsVersion = 11.5; 349 | }; 350 | }; 351 | }; 352 | buildConfigurationList = 3BA01F142482CFAD002F82DF /* Build configuration list for PBXProject "MetalTest" */; 353 | compatibilityVersion = "Xcode 9.3"; 354 | developmentRegion = en; 355 | hasScannedForEncodings = 0; 356 | knownRegions = ( 357 | en, 358 | Base, 359 | ); 360 | mainGroup = 3BA01F102482CFAD002F82DF; 361 | productRefGroup = 3BA01F1A2482CFAD002F82DF /* Products */; 362 | projectDirPath = ""; 363 | projectRoot = ""; 364 | targets = ( 365 | 3BA01F182482CFAD002F82DF /* MetalTest */, 366 | ); 367 | }; 368 | /* End PBXProject section */ 369 | 370 | /* Begin PBXResourcesBuildPhase section */ 371 | 3BA01F172482CFAD002F82DF /* Resources */ = { 372 | isa = PBXResourcesBuildPhase; 373 | buildActionMask = 2147483647; 374 | files = ( 375 | 3B7ADA18248938D000CA5AA3 /* TextureSamplingViewController.xib in Resources */, 376 | 3B4DB89C248E0E5E004E14ED /* ThreeDimentionsTransformViewController.xib in Resources */, 377 | 3B6CB70C2487A8050094E14A /* avatar.JPG in Resources */, 378 | 3BD530FD248F68D700E11997 /* LICENSE in Resources */, 379 | 3BD530E6248E444500E11997 /* RotatingCubeViewController.xib in Resources */, 380 | 3B7ADA1E2489390B00CA5AA3 /* FilterChainViewController.xib in Resources */, 381 | 3BD530FE248F68D700E11997 /* README.md in Resources */, 382 | 3BA01F292482CFB2002F82DF /* Assets.xcassets in Resources */, 383 | ); 384 | runOnlyForDeploymentPostprocessing = 0; 385 | }; 386 | /* End PBXResourcesBuildPhase section */ 387 | 388 | /* Begin PBXSourcesBuildPhase section */ 389 | 3BA01F152482CFAD002F82DF /* Sources */ = { 390 | isa = PBXSourcesBuildPhase; 391 | buildActionMask = 2147483647; 392 | files = ( 393 | 3B7B70CD2483D06400A0D86B /* TriangleViewController.m in Sources */, 394 | 3B6CB70E2487ABD90094E14A /* DrawImage.metal in Sources */, 395 | 3BB42A38248F90C5003C62B7 /* RenderCameraYUV.metal in Sources */, 396 | 3BB42A36248F90B3003C62B7 /* RenderCameraYUVViewController.m in Sources */, 397 | 3BD530EE248F619D00E11997 /* RenderCameraBGRA.metal in Sources */, 398 | 3B4DB89B248E0E5E004E14ED /* ThreeDimentionsTransformViewController.m in Sources */, 399 | 3BACD390249232D7008C4C6D /* ComputeShader.metal in Sources */, 400 | 3B7348D3249624000076E133 /* Blending.metal in Sources */, 401 | 3BA01F242482CFAD002F82DF /* ViewController.m in Sources */, 402 | 3BB42A622490DA83003C62B7 /* PhongLight.metal in Sources */, 403 | 3B7B70D02483D7A000A0D86B /* Triangle.metal in Sources */, 404 | 3BD530FC248F68D700E11997 /* MetalMatrix.m in Sources */, 405 | 3B7ADA1D2489390B00CA5AA3 /* FilterChainViewController.m in Sources */, 406 | 3BACD38E249231C2008C4C6D /* ComputeShaderViewController.m in Sources */, 407 | 3BD530EC248F3F5B00E11997 /* RenderCameraBGRAViewController.m in Sources */, 408 | 3B7ADA16248938D000CA5AA3 /* TextureSamplingViewController.m in Sources */, 409 | 3B9A250F249536B100D03D27 /* BlendingViewController.m in Sources */, 410 | 3B7ADA17248938D000CA5AA3 /* TextureSampling.metal in Sources */, 411 | 3B4DB8A1248E2400004E14ED /* MetalUtils.m in Sources */, 412 | 3BA01F1E2482CFAD002F82DF /* AppDelegate.m in Sources */, 413 | 3BA01F2F2482CFB2002F82DF /* main.m in Sources */, 414 | 3B6CB7092487A7DC0094E14A /* DrawImageViewController.m in Sources */, 415 | 3BD530E5248E444500E11997 /* RotatingCubeViewController.m in Sources */, 416 | 3BB42A3D2490B7D9003C62B7 /* PhongLightViewController.m in Sources */, 417 | 3B4DB89E248E0FCC004E14ED /* ThreeDimentionsTransform.metal in Sources */, 418 | 3B7ADA2424893B7900CA5AA3 /* FilterChain.metal in Sources */, 419 | 3BD530E8248E445300E11997 /* RotatingCube.metal in Sources */, 420 | ); 421 | runOnlyForDeploymentPostprocessing = 0; 422 | }; 423 | /* End PBXSourcesBuildPhase section */ 424 | 425 | /* Begin XCBuildConfiguration section */ 426 | 3BA01F302482CFB2002F82DF /* Debug */ = { 427 | isa = XCBuildConfiguration; 428 | buildSettings = { 429 | ALWAYS_SEARCH_USER_PATHS = NO; 430 | CLANG_ANALYZER_NONNULL = YES; 431 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 432 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 433 | CLANG_CXX_LIBRARY = "libc++"; 434 | CLANG_ENABLE_MODULES = YES; 435 | CLANG_ENABLE_OBJC_ARC = YES; 436 | CLANG_ENABLE_OBJC_WEAK = YES; 437 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 438 | CLANG_WARN_BOOL_CONVERSION = YES; 439 | CLANG_WARN_COMMA = YES; 440 | CLANG_WARN_CONSTANT_CONVERSION = YES; 441 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 442 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 443 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 444 | CLANG_WARN_EMPTY_BODY = YES; 445 | CLANG_WARN_ENUM_CONVERSION = YES; 446 | CLANG_WARN_INFINITE_RECURSION = YES; 447 | CLANG_WARN_INT_CONVERSION = YES; 448 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 449 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 450 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 451 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 452 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 453 | CLANG_WARN_STRICT_PROTOTYPES = YES; 454 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 455 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 456 | CLANG_WARN_UNREACHABLE_CODE = YES; 457 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 458 | COPY_PHASE_STRIP = NO; 459 | DEBUG_INFORMATION_FORMAT = dwarf; 460 | ENABLE_STRICT_OBJC_MSGSEND = YES; 461 | ENABLE_TESTABILITY = YES; 462 | GCC_C_LANGUAGE_STANDARD = gnu11; 463 | GCC_DYNAMIC_NO_PIC = NO; 464 | GCC_NO_COMMON_BLOCKS = YES; 465 | GCC_OPTIMIZATION_LEVEL = 0; 466 | GCC_PREPROCESSOR_DEFINITIONS = ( 467 | "DEBUG=1", 468 | "$(inherited)", 469 | ); 470 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 471 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 472 | GCC_WARN_UNDECLARED_SELECTOR = YES; 473 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 474 | GCC_WARN_UNUSED_FUNCTION = YES; 475 | GCC_WARN_UNUSED_VARIABLE = YES; 476 | IPHONEOS_DEPLOYMENT_TARGET = 13.5; 477 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 478 | MTL_FAST_MATH = YES; 479 | ONLY_ACTIVE_ARCH = YES; 480 | SDKROOT = iphoneos; 481 | }; 482 | name = Debug; 483 | }; 484 | 3BA01F312482CFB2002F82DF /* Release */ = { 485 | isa = XCBuildConfiguration; 486 | buildSettings = { 487 | ALWAYS_SEARCH_USER_PATHS = NO; 488 | CLANG_ANALYZER_NONNULL = YES; 489 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 490 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 491 | CLANG_CXX_LIBRARY = "libc++"; 492 | CLANG_ENABLE_MODULES = YES; 493 | CLANG_ENABLE_OBJC_ARC = YES; 494 | CLANG_ENABLE_OBJC_WEAK = YES; 495 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 496 | CLANG_WARN_BOOL_CONVERSION = YES; 497 | CLANG_WARN_COMMA = YES; 498 | CLANG_WARN_CONSTANT_CONVERSION = YES; 499 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 500 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 501 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 502 | CLANG_WARN_EMPTY_BODY = YES; 503 | CLANG_WARN_ENUM_CONVERSION = YES; 504 | CLANG_WARN_INFINITE_RECURSION = YES; 505 | CLANG_WARN_INT_CONVERSION = YES; 506 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 507 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 508 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 509 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 510 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 511 | CLANG_WARN_STRICT_PROTOTYPES = YES; 512 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 513 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 514 | CLANG_WARN_UNREACHABLE_CODE = YES; 515 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 516 | COPY_PHASE_STRIP = NO; 517 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 518 | ENABLE_NS_ASSERTIONS = NO; 519 | ENABLE_STRICT_OBJC_MSGSEND = YES; 520 | GCC_C_LANGUAGE_STANDARD = gnu11; 521 | GCC_NO_COMMON_BLOCKS = YES; 522 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 523 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 524 | GCC_WARN_UNDECLARED_SELECTOR = YES; 525 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 526 | GCC_WARN_UNUSED_FUNCTION = YES; 527 | GCC_WARN_UNUSED_VARIABLE = YES; 528 | IPHONEOS_DEPLOYMENT_TARGET = 13.5; 529 | MTL_ENABLE_DEBUG_INFO = NO; 530 | MTL_FAST_MATH = YES; 531 | SDKROOT = iphoneos; 532 | VALIDATE_PRODUCT = YES; 533 | }; 534 | name = Release; 535 | }; 536 | 3BA01F332482CFB2002F82DF /* Debug */ = { 537 | isa = XCBuildConfiguration; 538 | buildSettings = { 539 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 540 | CODE_SIGN_STYLE = Automatic; 541 | DEVELOPMENT_TEAM = NYQACL8B77; 542 | INFOPLIST_FILE = MetalTest/Info.plist; 543 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 544 | LD_RUNPATH_SEARCH_PATHS = ( 545 | "$(inherited)", 546 | "@executable_path/Frameworks", 547 | ); 548 | PRODUCT_BUNDLE_IDENTIFIER = com.kedc.MetalTest; 549 | PRODUCT_NAME = "$(TARGET_NAME)"; 550 | TARGETED_DEVICE_FAMILY = "1,2"; 551 | }; 552 | name = Debug; 553 | }; 554 | 3BA01F342482CFB2002F82DF /* Release */ = { 555 | isa = XCBuildConfiguration; 556 | buildSettings = { 557 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 558 | CODE_SIGN_STYLE = Automatic; 559 | DEVELOPMENT_TEAM = NYQACL8B77; 560 | INFOPLIST_FILE = MetalTest/Info.plist; 561 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 562 | LD_RUNPATH_SEARCH_PATHS = ( 563 | "$(inherited)", 564 | "@executable_path/Frameworks", 565 | ); 566 | PRODUCT_BUNDLE_IDENTIFIER = com.kedc.MetalTest; 567 | PRODUCT_NAME = "$(TARGET_NAME)"; 568 | TARGETED_DEVICE_FAMILY = "1,2"; 569 | }; 570 | name = Release; 571 | }; 572 | /* End XCBuildConfiguration section */ 573 | 574 | /* Begin XCConfigurationList section */ 575 | 3BA01F142482CFAD002F82DF /* Build configuration list for PBXProject "MetalTest" */ = { 576 | isa = XCConfigurationList; 577 | buildConfigurations = ( 578 | 3BA01F302482CFB2002F82DF /* Debug */, 579 | 3BA01F312482CFB2002F82DF /* Release */, 580 | ); 581 | defaultConfigurationIsVisible = 0; 582 | defaultConfigurationName = Release; 583 | }; 584 | 3BA01F322482CFB2002F82DF /* Build configuration list for PBXNativeTarget "MetalTest" */ = { 585 | isa = XCConfigurationList; 586 | buildConfigurations = ( 587 | 3BA01F332482CFB2002F82DF /* Debug */, 588 | 3BA01F342482CFB2002F82DF /* Release */, 589 | ); 590 | defaultConfigurationIsVisible = 0; 591 | defaultConfigurationName = Release; 592 | }; 593 | /* End XCConfigurationList section */ 594 | }; 595 | rootObject = 3BA01F112482CFAD002F82DF /* Project object */; 596 | } 597 | -------------------------------------------------------------------------------- /MetalTest/MetalTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MetalTest/MetalTest.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MetalTest/MetalTest.xcodeproj/xcshareddata/xcschemes/MetalTest.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 43 | 45 | 51 | 52 | 53 | 54 | 58 | 59 | 60 | 61 | 67 | 69 | 75 | 76 | 77 | 78 | 80 | 81 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (nonatomic, strong) UIWindow * window; 14 | 15 | @end 16 | 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | 20 | UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: [NSClassFromString(@"ViewController") new]]; 21 | 22 | self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds]; 23 | self.window.rootViewController = nav; 24 | [self.window makeKeyAndVisible]; 25 | return YES; 26 | } 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NSCameraUsageDescription 6 | Use camera 7 | CFBundleDevelopmentRegion 8 | $(DEVELOPMENT_LANGUAGE) 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @property (nonatomic, strong) UITableView * table; 14 | 15 | @property (nonatomic, copy) NSArray *> * demoArray; 16 | 17 | @end 18 | 19 | NSString * const kCellId = @"cellId"; 20 | 21 | @implementation ViewController 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | 26 | _table = [[UITableView alloc] initWithFrame: self.view.bounds style: UITableViewStylePlain]; 27 | _table.delegate = self; 28 | _table.dataSource = self; 29 | [self.view addSubview: _table]; 30 | 31 | [_table registerClass: [UITableViewCell class] forCellReuseIdentifier: kCellId]; 32 | 33 | _demoArray = @ [ 34 | @{@"1. 三角形": @"TriangleViewController"}, 35 | @{@"2. 画一张图片": @"DrawImageViewController"}, 36 | @{@"3. 纹理采样参数详解": @"TextureSamplingViewController"}, 37 | @{@"4. 滤镜链": @"FilterChainViewController"}, 38 | @{@"5. 三维变换": @"ThreeDimentionsTransformViewController"}, 39 | @{@"6. 旋转的立方体": @"RotatingCubeViewController"}, 40 | @{@"7. 渲染摄像头采集的 RGBA 数据(CVMetalTextureCacheRef)": @"RenderCameraBGRAViewController"}, 41 | @{@"8. 渲染摄像头采集的 YUV(YCbCr)数据": @"RenderCameraYUVViewController"}, 42 | @{@"9. 光照(冯氏光照模型)": @"PhongLightViewController"}, 43 | @{@"10. 并行计算(Compute Shader)": @"ComputeShaderViewController"}, 44 | @{@"11. 混合 blend": @"BlendingViewController"}, 45 | ]; 46 | } 47 | 48 | - (void)viewWillAppear:(BOOL)animated { 49 | [super viewWillAppear: animated]; 50 | 51 | self.title = @"Demo 列表"; 52 | } 53 | 54 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 55 | 56 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellId]; 57 | 58 | if (cell == nil) { 59 | cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: kCellId]; 60 | } 61 | 62 | cell.textLabel.text = _demoArray[indexPath.item].allKeys.firstObject; 63 | 64 | return cell; 65 | } 66 | 67 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 68 | return 1; 69 | } 70 | 71 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 72 | return _demoArray.count; 73 | } 74 | 75 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 76 | 77 | NSString *vcName = _demoArray[indexPath.item].allValues.firstObject; 78 | UIViewController *vc = [NSClassFromString(vcName) new]; 79 | vc.title = _demoArray[indexPath.item].allKeys.firstObject; 80 | [self.navigationController pushViewController: vc animated:true]; 81 | } 82 | 83 | 84 | @end 85 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | NSString * appDelegateClassName; 14 | @autoreleasepool { 15 | // Setup code that might create autoreleased objects goes here. 16 | appDelegateClassName = NSStringFromClass([AppDelegate class]); 17 | } 18 | return UIApplicationMain(argc, argv, nil, appDelegateClassName); 19 | } 20 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/resources/avatar.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceKim/MetalTest/e68d8ff9ff9270e64a037131a6ffbc911a6e36f6/MetalTest/MetalTest/resources/avatar.JPG -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/1. 三角形/Triangle.metal: -------------------------------------------------------------------------------- 1 | // 2 | // Triangle.metal 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | typedef struct 13 | { 14 | // The [[position]] attribute of this member indicates that this value 15 | // is the clip space position of the vertex when this structure is 16 | // returned from the vertex function. 17 | float4 position [[position]]; 18 | 19 | // Since this member does not have a special attribute, the rasterizer 20 | // interpolates its value with the values of the other triangle vertices 21 | // and then passes the interpolated value to the fragment shader for each 22 | // fragment in the triangle. 23 | float4 color; 24 | 25 | } VertexOut; 26 | 27 | vertex VertexOut 28 | vertexShader(uint vertexID [[ vertex_id ]], 29 | constant float4 *position [[ buffer(0) ]], 30 | constant float4 *color [[ buffer(1) ]] 31 | ) 32 | { 33 | VertexOut out; 34 | 35 | out.position = position[vertexID]; 36 | out.color = color[vertexID]; 37 | 38 | return out; 39 | } 40 | 41 | fragment float4 42 | fragmentShader(VertexOut fragmentIn [[ stage_in ]]) 43 | { 44 | return fragmentIn.color; 45 | } 46 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/1. 三角形/TriangleViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // TriangleViewController.h 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | API_AVAILABLE(ios(13.0)) 14 | @interface TriangleViewController : UIViewController 15 | 16 | @end 17 | 18 | NS_ASSUME_NONNULL_END 19 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/1. 三角形/TriangleViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // TriangleViewController.m 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/5/31. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import "TriangleViewController.h" 10 | 11 | #import 12 | 13 | // 使用 float4 防止对齐的问题 14 | static const float vertices[] = { 15 | 0, 0.5, 0, 1, 16 | -0.5, -0.5, 0, 1, 17 | 0.5, -0.5, 0, 1, 18 | }; 19 | 20 | static const float color_data[] = { 21 | 1, 0, 0, 1, 22 | 0, 1, 0, 1, 23 | 0, 0, 1, 1, 24 | }; 25 | 26 | @interface TriangleViewController () 27 | 28 | @end 29 | 30 | @implementation TriangleViewController { 31 | 32 | CADisplayLink *_dis; 33 | 34 | id _device; 35 | 36 | id _queue; 37 | 38 | CAMetalLayer *_layer; 39 | 40 | MTLRenderPassDescriptor *_renderTargetDesc; 41 | 42 | id _renderPipelineState; 43 | } 44 | 45 | #pragma mark - Life Circle 46 | 47 | - (void)viewDidLoad { 48 | [super viewDidLoad]; 49 | 50 | _dis = [CADisplayLink displayLinkWithTarget: self selector: @selector(render)]; 51 | } 52 | 53 | - (void)viewDidAppear:(BOOL)animated { 54 | [super viewDidAppear: animated]; 55 | 56 | [self setupMetal]; 57 | [self setupLayer]; 58 | [self setupRenderTarget]; 59 | [self setupRenderPipeline]; 60 | 61 | [self render]; 62 | // [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 63 | } 64 | 65 | #pragma mark - Metal 66 | 67 | - (void)setupMetal { 68 | 69 | _device = MTLCreateSystemDefaultDevice(); 70 | 71 | _queue = [_device newCommandQueue]; 72 | } 73 | 74 | - (void)setupLayer { 75 | 76 | _layer = [CAMetalLayer layer]; 77 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 78 | _layer.framebufferOnly = true; 79 | _layer.frame = self.view.bounds; 80 | 81 | CGFloat scale = self.view.contentScaleFactor; 82 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 83 | 84 | [self.view.layer addSublayer: _layer]; 85 | } 86 | 87 | - (void)setupRenderTarget { 88 | 89 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 90 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 91 | // colorAttachment.texture = [_layer nextDrawable].texture; 92 | colorAttachment.loadAction = MTLLoadActionClear; 93 | colorAttachment.storeAction = MTLStoreActionStore; 94 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 95 | } 96 | 97 | - (void)setupRenderPipeline { 98 | 99 | id library = [_device newDefaultLibrary]; 100 | 101 | id vertexFunc = [library newFunctionWithName: @"vertexShader"]; 102 | id fragmentFunc = [library newFunctionWithName: @"fragmentShader"]; 103 | 104 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 105 | pipelineDescriptor.label = @"Render Pipeline"; 106 | pipelineDescriptor.vertexFunction = vertexFunc; 107 | pipelineDescriptor.fragmentFunction = fragmentFunc; 108 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 109 | 110 | NSError *err; 111 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 112 | 113 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 114 | } 115 | 116 | - (void)render { 117 | 118 | id currentDrawable = [_layer nextDrawable]; 119 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 120 | 121 | id commandBuffer = [_queue commandBuffer]; 122 | commandBuffer.label = @"Command Buffer"; 123 | 124 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 125 | encoder.label = @"Render Command Encoder"; 126 | 127 | [encoder setViewport: (MTLViewport) { 128 | .originX = 0, 129 | .originY = 0, 130 | .width = _layer.drawableSize.width, 131 | .height = _layer.drawableSize.height, 132 | .znear = 0, 133 | .zfar = 1 134 | }]; 135 | 136 | [encoder setRenderPipelineState: _renderPipelineState]; 137 | 138 | [encoder setVertexBytes: vertices 139 | length: sizeof(vertices) 140 | atIndex: 0]; 141 | 142 | [encoder setVertexBytes: color_data 143 | length: sizeof(color_data) 144 | atIndex: 1]; 145 | 146 | [encoder drawPrimitives: MTLPrimitiveTypeTriangle 147 | vertexStart: 0 148 | vertexCount: 3]; 149 | 150 | [encoder endEncoding]; 151 | 152 | [commandBuffer presentDrawable: currentDrawable]; 153 | 154 | [commandBuffer commit]; 155 | } 156 | 157 | @end 158 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/10. GPGPU通用计算(Compute Shader)/ComputeShader.metal: -------------------------------------------------------------------------------- 1 | //// ComputeShader.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/11. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | kernel 12 | void ComputeKernelShader(device uint *outBuffer [[ buffer(0) ]], 13 | constant uint2 &outSize [[ buffer(1) ]], 14 | uint2 position [[ thread_position_in_grid ]]) { 15 | 16 | if (position.x >= outSize.x || position.y >= outSize.y) { 17 | return; 18 | } 19 | 20 | uint idx = position.y * outSize.x + position.x; 21 | outBuffer[idx] = idx; 22 | } 23 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/10. GPGPU通用计算(Compute Shader)/ComputeShaderViewController.h: -------------------------------------------------------------------------------- 1 | //// ComputeShaderViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/11. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface ComputeShaderViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/10. GPGPU通用计算(Compute Shader)/ComputeShaderViewController.m: -------------------------------------------------------------------------------- 1 | //// ComputeShaderViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/11. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "ComputeShaderViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | #import 13 | 14 | @interface ComputeShaderViewController () 15 | 16 | @end 17 | 18 | @implementation ComputeShaderViewController { 19 | 20 | NSUInteger _outW, _outH; 21 | 22 | id _device; 23 | 24 | id _queue; 25 | 26 | id _computePipelineState; 27 | 28 | MTLSize _threadgroupsPerGrid; // 每个计算网格的 线程组 size 29 | MTLSize _threadsPerThreadGroup; // 每个线程组的 线程 size 30 | 31 | id _outputBuffer; 32 | 33 | } 34 | 35 | - (void)viewDidLoad { 36 | [super viewDidLoad]; 37 | 38 | self.view.backgroundColor = UIColor.whiteColor; 39 | 40 | _outW = 1024; 41 | _outH = 1024; 42 | 43 | [self setupMetal]; 44 | [self setupComputePipeline]; 45 | [self createOutputBuffer]; 46 | [self setupThreadGroups: _outW totalHeight: _outH]; 47 | [self compute]; 48 | 49 | for (NSUInteger y = 0; y < 3; y++) { 50 | for (NSUInteger x = 0; x < 3; x++) { 51 | [self showBufferContentWithX: x withY: y]; 52 | } 53 | } 54 | } 55 | 56 | #pragma mark - Result Verifying 57 | 58 | - (void)showBufferContentWithX: (NSUInteger)x 59 | withY: (NSUInteger)y { 60 | 61 | uint32_t *res = _outputBuffer.contents; 62 | 63 | uint32_t resOfIdx = res[y * _outW + x]; 64 | NSLog(@"The result of (%d, %d) is - %d", (int)x, (int)y, resOfIdx); 65 | } 66 | 67 | #pragma mark - Metal 68 | 69 | - (void)setupMetal { 70 | 71 | _device = MTLCreateSystemDefaultDevice(); 72 | 73 | _queue = [_device newCommandQueue]; 74 | } 75 | 76 | - (void)setupComputePipeline { 77 | 78 | id library = [_device newDefaultLibrary]; 79 | 80 | id kernelFunc = [library newFunctionWithName: @"ComputeKernelShader"]; 81 | 82 | NSError *err; 83 | _computePipelineState = [_device newComputePipelineStateWithFunction: kernelFunc error: &err]; 84 | 85 | NSAssert(_computePipelineState != nil, @"Failed to create pipeline state: %@", err); 86 | } 87 | 88 | - (void)createOutputBuffer { 89 | 90 | _outputBuffer = [_device newBufferWithLength: _outW * _outH * sizeof(uint32_t) 91 | options: MTLResourceStorageModeShared]; 92 | } 93 | 94 | - (void)setupThreadGroups: (NSUInteger)totalWidth totalHeight: (NSUInteger)totalHeight { 95 | 96 | // Refer apple doc 97 | // https://developer.apple.com/documentation/metal/calculating_threadgroup_and_grid_sizes?language=objc 98 | 99 | NSUInteger w = _computePipelineState.threadExecutionWidth; // 最有效率的线程执行宽度 100 | NSUInteger h = _computePipelineState.maxTotalThreadsPerThreadgroup / w; // 每个线程组最多的线程数量 101 | 102 | _threadsPerThreadGroup = MTLSizeMake(w, 103 | h, 104 | 1); 105 | 106 | _threadgroupsPerGrid = MTLSizeMake((totalWidth + w - 1) / w, 107 | (totalHeight + h - 1) / h, 108 | 1); 109 | } 110 | 111 | - (void)compute { 112 | 113 | simd_uint2 outSize = simd_make_uint2((uint32_t)_outW, (uint32_t)_outH); 114 | 115 | id commandBuffer = [_queue commandBuffer]; 116 | 117 | id encoder = [commandBuffer computeCommandEncoder]; 118 | 119 | [encoder setComputePipelineState: _computePipelineState]; 120 | 121 | [encoder setBuffer: _outputBuffer offset: 0 atIndex: 0]; 122 | 123 | [encoder setBytes: &outSize length: sizeof(simd_uint2) atIndex: 1]; 124 | 125 | [encoder dispatchThreadgroups: _threadgroupsPerGrid threadsPerThreadgroup: _threadsPerThreadGroup]; 126 | 127 | [encoder endEncoding]; 128 | 129 | 130 | [commandBuffer addCompletedHandler:^(id _Nonnull cmd) { 131 | NSLog(@"Finish Computing."); 132 | }]; 133 | 134 | [commandBuffer commit]; 135 | 136 | [commandBuffer waitUntilCompleted]; 137 | } 138 | 139 | @end 140 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/11.混合/Blending.metal: -------------------------------------------------------------------------------- 1 | // 2 | // Blending.metal 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/6/14. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | typedef struct 13 | { 14 | float4 position [[position]]; 15 | float2 texCoor; 16 | 17 | } VertexOut; 18 | 19 | vertex VertexOut 20 | BlendingVertexShader( 21 | uint vertexID [[ vertex_id ]], 22 | constant packed_float3 *position [[ buffer(0) ]], 23 | constant packed_float2 *texCoor [[ buffer(1) ]] 24 | ) { 25 | VertexOut out; 26 | 27 | out.position = float4(position[vertexID], 1); 28 | out.texCoor = texCoor[vertexID]; 29 | 30 | return out; 31 | } 32 | 33 | fragment float4 34 | BlendingFragmentShader( 35 | VertexOut in [[ stage_in ]], 36 | texture2d tex [[ texture(0) ]] 37 | ) { 38 | constexpr sampler texSampler; 39 | float4 color = tex.sample(texSampler, in.texCoor); 40 | return color; 41 | } 42 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/11.混合/BlendingViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // BlendingViewController.h 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/6/14. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface BlendingViewController : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/11.混合/BlendingViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // BlendingViewController.m 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/6/14. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import "BlendingViewController.h" 10 | 11 | #import "MetalUtils.h" 12 | 13 | // 左上顶点 14 | static const float vertices0[] = { 15 | -1, -0.25, 0, // 左下角 16 | 0.25, -0.25, 0, // 右下角 17 | -1, 1, 0, // 左上角 18 | 0.25, 1, 0 // 右上角 19 | }; 20 | 21 | // 右下顶点 22 | static const float vertices1[] = { 23 | -0.25, -1, 0, // 左下角 24 | 1, -1, 0, // 右下角 25 | -0.25, 0.25, 0, // 左上角 26 | 1, 0.25, 0 // 右上角 27 | }; 28 | 29 | static const float texCoor[] = { 30 | 0, 0, // 左下角 31 | 1, 0, // 右下角 32 | 0, 1, // 左上角 33 | 1, 1, // 右上角 34 | }; 35 | 36 | static const UInt32 indices[] = { 37 | 0, 1, 2, 38 | 1, 3, 2 39 | }; 40 | 41 | @interface BlendingViewController () 42 | 43 | @end 44 | 45 | @implementation BlendingViewController { 46 | 47 | UIImage *_redImage, *_greenImage; 48 | 49 | id _device; 50 | 51 | id _queue; 52 | 53 | CAMetalLayer *_layer; 54 | 55 | MTLRenderPassDescriptor *_renderTargetDesc; 56 | 57 | id _renderPipelineState0, _renderPipelineState1; 58 | 59 | id _texture0, _texture1; 60 | 61 | id _indexBuffer; 62 | } 63 | 64 | #pragma mark - Life Circle 65 | 66 | - (void)viewDidLoad { 67 | [super viewDidLoad]; 68 | 69 | self.view.backgroundColor = UIColor.whiteColor; 70 | 71 | _redImage = [self createPureColorImage: UIColor.redColor alpha: 1]; // 红色不透明 72 | _greenImage = [self createPureColorImage: UIColor.greenColor alpha: 0.5]; // 绿色是半透明的 73 | } 74 | 75 | - (void)viewDidAppear:(BOOL)animated { 76 | [super viewDidAppear: animated]; 77 | 78 | [self setupMetal]; 79 | [self setupLayer]; 80 | [self setupRenderTarget]; 81 | [self setupRenderPipelines]; 82 | [self loadTextures]; 83 | [self setupIndexBuffer]; 84 | 85 | [self render]; 86 | // [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 87 | } 88 | 89 | #pragma mark - Create pure color images 90 | 91 | - (UIImage *)createPureColorImage: (UIColor *)color 92 | alpha: (CGFloat)alpha { 93 | 94 | UIColor *colorWithAlpha = [color colorWithAlphaComponent: alpha]; 95 | 96 | CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 97 | UIGraphicsBeginImageContextWithOptions(rect.size, false, 0); 98 | CGContextRef context = UIGraphicsGetCurrentContext(); 99 | CGContextSetFillColorWithColor(context, [colorWithAlpha CGColor]); 100 | CGContextFillRect(context, rect); 101 | UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 102 | UIGraphicsEndImageContext(); 103 | return theImage; 104 | } 105 | 106 | #pragma mark - Metal 107 | 108 | - (void)setupMetal { 109 | 110 | _device = MTLCreateSystemDefaultDevice(); 111 | 112 | _queue = [_device newCommandQueue]; 113 | } 114 | 115 | - (void)setupLayer { 116 | 117 | _layer = [CAMetalLayer layer]; 118 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 119 | _layer.framebufferOnly = true; 120 | _layer.frame = self.view.bounds; 121 | 122 | CGFloat scale = self.view.contentScaleFactor; 123 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 124 | 125 | [self.view.layer insertSublayer: _layer atIndex: 0]; 126 | } 127 | 128 | - (void)setupRenderTarget { 129 | 130 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 131 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 132 | // colorAttachment.texture = [_layer nextDrawable].texture; 133 | colorAttachment.loadAction = MTLLoadActionClear; 134 | colorAttachment.storeAction = MTLStoreActionStore; 135 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 136 | } 137 | 138 | - (void)setupRenderPipelines { 139 | 140 | id library = [_device newDefaultLibrary]; 141 | 142 | id vertexFunc = [library newFunctionWithName: @"BlendingVertexShader"]; 143 | id fragmentFunc = [library newFunctionWithName: @"BlendingFragmentShader"]; 144 | 145 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 146 | pipelineDescriptor.label = @"Render Pipeline"; 147 | pipelineDescriptor.vertexFunction = vertexFunc; 148 | pipelineDescriptor.fragmentFunction = fragmentFunc; 149 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 150 | 151 | // Red image pipeline state 152 | NSError *err; 153 | _renderPipelineState0 = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 154 | 155 | NSAssert(_renderPipelineState0 != nil, @"Failed to create pipeline state: %@", err); 156 | 157 | // Green image pipeline state 158 | 159 | // 1. setup color & alpha factor 160 | pipelineDescriptor.colorAttachments[0].blendingEnabled = true; 161 | pipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd; 162 | pipelineDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd; 163 | pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha; 164 | pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha; 165 | pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha; 166 | pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha; 167 | 168 | // 2. Use new descriptor to generate green pipeline state 169 | _renderPipelineState1 = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 170 | 171 | NSAssert(_renderPipelineState1 != nil, @"Failed to create pipeline state: %@", err); 172 | } 173 | 174 | - (void)loadTextures { 175 | _texture0 = [MetalUtils loadImageTexture: _redImage device: _device usage: MTLTextureUsageShaderRead]; 176 | _texture1 = [MetalUtils loadImageTexture: _greenImage device: _device usage: MTLTextureUsageShaderRead]; 177 | } 178 | 179 | - (void)setupIndexBuffer { 180 | 181 | _indexBuffer = [_device newBufferWithBytes: indices 182 | length: sizeof(indices) 183 | options: MTLResourceStorageModeShared]; 184 | } 185 | 186 | - (void)render_red: (id)encoder { 187 | 188 | [encoder setRenderPipelineState: _renderPipelineState0]; 189 | 190 | [encoder setVertexBytes: vertices0 191 | length: sizeof(vertices0) 192 | atIndex: 0]; 193 | 194 | [encoder setVertexBytes: texCoor 195 | length: sizeof(texCoor) 196 | atIndex: 1]; 197 | 198 | [encoder setFragmentTexture: _texture0 199 | atIndex: 0]; 200 | 201 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangle 202 | indexCount: 6 203 | indexType: MTLIndexTypeUInt32 204 | indexBuffer: _indexBuffer 205 | indexBufferOffset: 0]; 206 | } 207 | 208 | - (void)render_green: (id)encoder { 209 | 210 | [encoder setRenderPipelineState: _renderPipelineState1]; 211 | 212 | [encoder setVertexBytes: vertices1 213 | length: sizeof(vertices1) 214 | atIndex: 0]; 215 | 216 | [encoder setVertexBytes: texCoor 217 | length: sizeof(texCoor) 218 | atIndex: 1]; 219 | 220 | [encoder setFragmentTexture: _texture1 221 | atIndex: 0]; 222 | 223 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangle 224 | indexCount: 6 225 | indexType: MTLIndexTypeUInt32 226 | indexBuffer: _indexBuffer 227 | indexBufferOffset: 0]; 228 | } 229 | 230 | - (void)render { 231 | id currentDrawable = [_layer nextDrawable]; 232 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 233 | 234 | id commandBuffer = [_queue commandBuffer]; 235 | commandBuffer.label = @"Command Buffer"; 236 | 237 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 238 | encoder.label = @"Render Command Encoder"; 239 | 240 | [encoder setViewport: (MTLViewport) { 241 | .originX = 0, 242 | .originY = 0, 243 | .width = _layer.drawableSize.width, 244 | .height = _layer.drawableSize.height, 245 | .znear = 0, 246 | .zfar = 1 247 | }]; 248 | 249 | [self render_red: encoder]; 250 | 251 | [self render_green: encoder]; 252 | 253 | [encoder endEncoding]; 254 | 255 | [commandBuffer presentDrawable: currentDrawable]; 256 | 257 | [commandBuffer commit]; 258 | } 259 | 260 | 261 | @end 262 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/2. 画一张图片/DrawImage.metal: -------------------------------------------------------------------------------- 1 | //// DrawImage.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/3. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | typedef struct 12 | { 13 | float4 position [[position]]; 14 | float2 texCoor; 15 | 16 | } VertexOut; 17 | 18 | vertex VertexOut 19 | DrawImageVertexShader( 20 | uint vertexID [[ vertex_id ]], 21 | constant float4 *position [[ buffer(0) ]], 22 | constant float2 *texCoor [[ buffer(1) ]] 23 | ) { 24 | VertexOut out; 25 | 26 | out.position = position[vertexID]; 27 | out.texCoor = texCoor[vertexID]; 28 | 29 | return out; 30 | } 31 | 32 | fragment float4 33 | DrawImageFragmentShader( 34 | VertexOut in [[ stage_in ]], 35 | texture2d tex [[ texture(0) ]] 36 | ) { 37 | constexpr sampler texSampler; 38 | float4 color = tex.sample(texSampler, in.texCoor); 39 | return color; 40 | } 41 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/2. 画一张图片/DrawImageViewController.h: -------------------------------------------------------------------------------- 1 | //// DrawImageViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/3. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface DrawImageViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/2. 画一张图片/DrawImageViewController.m: -------------------------------------------------------------------------------- 1 | //// DrawImageViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/3. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "DrawImageViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | @interface DrawImageViewController () 13 | 14 | @end 15 | 16 | @implementation DrawImageViewController { 17 | 18 | id _device; 19 | 20 | id _queue; 21 | 22 | CAMetalLayer *_layer; 23 | 24 | MTLRenderPassDescriptor *_renderTargetDesc; 25 | 26 | id _renderPipelineState; 27 | 28 | id _texutre; 29 | 30 | id _indexBuffer; 31 | } 32 | 33 | static const float vertices[] = { 34 | -0.5, -0.5, 0, 1, // 左下角 35 | 0.5, -0.5, 0, 1, // 右下角 36 | -0.5, 0.5, 0, 1, // 左上角 37 | 0.5, 0.5, 0, 1, // 右上角 38 | }; 39 | 40 | static const float texCoor[] = { 41 | 0, 0, // 左下角 42 | 1, 0, // 右下角 43 | 0, 1, // 左上角 44 | 1, 1, // 右上角 45 | }; 46 | 47 | static const UInt32 indices[] = { 48 | 0, 1, 2, 49 | 1, 3, 2 50 | }; 51 | 52 | #pragma mark - Life Circle 53 | 54 | - (void)viewDidLoad { 55 | [super viewDidLoad]; 56 | } 57 | 58 | - (void)viewDidAppear:(BOOL)animated { 59 | [super viewDidAppear: animated]; 60 | 61 | [self setupMetal]; 62 | [self setupLayer]; 63 | [self setupRenderTarget]; 64 | [self setupRenderPipeline]; 65 | [self loadTexture]; 66 | [self setupIndexBuffer]; 67 | 68 | [self render]; 69 | // [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 70 | } 71 | 72 | #pragma mark - Metal 73 | 74 | - (void)setupMetal { 75 | 76 | _device = MTLCreateSystemDefaultDevice(); 77 | 78 | _queue = [_device newCommandQueue]; 79 | } 80 | 81 | - (void)setupLayer { 82 | 83 | _layer = [CAMetalLayer layer]; 84 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 85 | _layer.framebufferOnly = true; 86 | _layer.frame = self.view.bounds; 87 | 88 | CGFloat scale = self.view.contentScaleFactor; 89 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 90 | 91 | [self.view.layer insertSublayer: _layer atIndex: 0]; 92 | } 93 | 94 | - (void)setupRenderTarget { 95 | 96 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 97 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 98 | // colorAttachment.texture = [_layer nextDrawable].texture; 99 | colorAttachment.loadAction = MTLLoadActionClear; 100 | colorAttachment.storeAction = MTLStoreActionStore; 101 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 102 | } 103 | 104 | - (void)setupRenderPipeline { 105 | 106 | id library = [_device newDefaultLibrary]; 107 | 108 | id vertexFunc = [library newFunctionWithName: @"DrawImageVertexShader"]; 109 | id fragmentFunc = [library newFunctionWithName: @"DrawImageFragmentShader"]; 110 | 111 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 112 | pipelineDescriptor.label = @"Render Pipeline"; 113 | pipelineDescriptor.vertexFunction = vertexFunc; 114 | pipelineDescriptor.fragmentFunction = fragmentFunc; 115 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 116 | 117 | NSError *err; 118 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 119 | 120 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 121 | } 122 | 123 | - (void)loadTexture { 124 | UIImage *image = [UIImage imageNamed: @"avatar.JPG"]; 125 | _texutre = [MetalUtils loadImageTexture: image device: _device usage: MTLTextureUsageShaderRead]; 126 | } 127 | 128 | - (void)setupIndexBuffer { 129 | 130 | _indexBuffer = [_device newBufferWithBytes: indices 131 | length: sizeof(indices) 132 | options: MTLResourceStorageModeShared]; 133 | } 134 | 135 | - (void)render { 136 | 137 | id currentDrawable = [_layer nextDrawable]; 138 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 139 | 140 | id commandBuffer = [_queue commandBuffer]; 141 | commandBuffer.label = @"Command Buffer"; 142 | 143 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 144 | encoder.label = @"Render Command Encoder"; 145 | 146 | [encoder setViewport: (MTLViewport) { 147 | .originX = 0, 148 | .originY = 0, 149 | .width = _layer.drawableSize.width, 150 | .height = _layer.drawableSize.height, 151 | .znear = 0, 152 | .zfar = 1 153 | }]; 154 | 155 | [encoder setRenderPipelineState: _renderPipelineState]; 156 | 157 | [encoder setVertexBytes: vertices 158 | length: sizeof(vertices) 159 | atIndex: 0]; 160 | 161 | [encoder setVertexBytes: texCoor 162 | length: sizeof(texCoor) 163 | atIndex: 1]; 164 | 165 | [encoder setFragmentTexture: _texutre 166 | atIndex: 0]; 167 | 168 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangle 169 | indexCount: 6 170 | indexType: MTLIndexTypeUInt32 171 | indexBuffer: _indexBuffer 172 | indexBufferOffset: 0]; 173 | 174 | [encoder endEncoding]; 175 | 176 | [commandBuffer presentDrawable: currentDrawable]; 177 | 178 | [commandBuffer commit]; 179 | } 180 | 181 | @end 182 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/3. 纹理采样参数详解/TextureSampling.metal: -------------------------------------------------------------------------------- 1 | //// TextureSampling.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/4. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | typedef struct 12 | { 13 | float4 position [[position]]; 14 | float2 texCoor; 15 | 16 | } VertexOut; 17 | 18 | //constexpr sampler texSampler( 19 | // coord::normalized, 20 | // address::repeat, 21 | // filter::linear 22 | // ); 23 | 24 | vertex VertexOut 25 | TextureSamplingVertexShader( 26 | uint vertexID [[ vertex_id ]], 27 | constant float4 *position [[ buffer(0) ]], 28 | constant float2 *texCoor [[ buffer(1) ]] 29 | ) { 30 | VertexOut out; 31 | 32 | out.position = position[vertexID]; 33 | out.texCoor = texCoor[vertexID]; 34 | 35 | return out; 36 | } 37 | 38 | fragment float4 39 | TextureSamplingFragmentShader( 40 | VertexOut in [[ stage_in ]], 41 | texture2d tex [[ texture(0) ]], 42 | sampler texSampler [[ sampler(0) ]] 43 | ) { 44 | float4 color = tex.sample(texSampler, in.texCoor); 45 | return color; 46 | } 47 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/3. 纹理采样参数详解/TextureSamplingViewController.h: -------------------------------------------------------------------------------- 1 | //// TextureSamplingViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/4. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface TextureSamplingViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/3. 纹理采样参数详解/TextureSamplingViewController.m: -------------------------------------------------------------------------------- 1 | //// TextureSamplingViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/4. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "TextureSamplingViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | @interface TextureSamplingViewController () 13 | 14 | @property (weak, nonatomic) IBOutlet UISegmentedControl *r_seg; 15 | @property (weak, nonatomic) IBOutlet UISegmentedControl *s_seg; 16 | @property (weak, nonatomic) IBOutlet UISegmentedControl *t_seg; 17 | 18 | @end 19 | 20 | @implementation TextureSamplingViewController { 21 | 22 | id _device; 23 | 24 | id _queue; 25 | 26 | CAMetalLayer *_layer; 27 | 28 | MTLRenderPassDescriptor *_renderTargetDesc; 29 | 30 | id _renderPipelineState; 31 | 32 | id _texutre; 33 | 34 | id _indexBuffer; 35 | 36 | id _samplerState; 37 | } 38 | 39 | static const float vertices[] = { 40 | -0.5, -0.5, 0, 1, // 左下角 41 | 0.5, -0.5, 0, 1, // 右下角 42 | -0.5, 0.5, 0, 1, // 左上角 43 | 0.5, 0.5, 0, 1, // 右上角 44 | }; 45 | 46 | // 正常而言,纹理坐标应该介于 [0, 1],这里为了测试,让其越界 47 | static const float texCoor[] = { 48 | -1, -1, // 左下角 49 | 1, -1, // 右下角 50 | -1, 1, // 左上角 51 | 1, 1, // 右上角 52 | }; 53 | 54 | static const UInt32 indices[] = { 55 | 0, 1, 2, 56 | 1, 3, 2 57 | }; 58 | 59 | #pragma mark - Life Circle 60 | 61 | - (void)viewDidLoad { 62 | [super viewDidLoad]; 63 | } 64 | 65 | - (void)viewDidAppear:(BOOL)animated { 66 | [super viewDidAppear: animated]; 67 | 68 | [self setupMetal]; 69 | [self setupLayer]; 70 | [self setupRenderTarget]; 71 | [self setupRenderPipeline]; 72 | [self loadTexture]; 73 | [self setupIndexBuffer]; 74 | [self setupSamplerState]; 75 | 76 | [self render]; 77 | // [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 78 | } 79 | 80 | #pragma mark - Response 81 | 82 | - (IBAction)onR_AddressSegmentChanged:(id)sender { 83 | [self setupSamplerState]; 84 | [self render]; 85 | } 86 | 87 | - (IBAction)onS_AddressSegmentChanged:(id)sender { 88 | [self setupSamplerState]; 89 | [self render]; 90 | } 91 | 92 | - (IBAction)onT_AddressSegmentChanged:(id)sender { 93 | [self setupSamplerState]; 94 | [self render]; 95 | } 96 | 97 | - (MTLSamplerAddressMode)getModeBySegment: (UISegmentedControl *)segment { 98 | if (segment.selectedSegmentIndex == 0) return MTLSamplerAddressModeClampToEdge; 99 | if (segment.selectedSegmentIndex == 1) return MTLSamplerAddressModeRepeat; 100 | if (segment.selectedSegmentIndex == 2) return MTLSamplerAddressModeMirrorRepeat; 101 | if (segment.selectedSegmentIndex == 3) return MTLSamplerAddressModeClampToZero; 102 | 103 | return MTLSamplerAddressModeClampToEdge; 104 | } 105 | 106 | #pragma mark - Metal 107 | 108 | - (void)setupMetal { 109 | 110 | _device = MTLCreateSystemDefaultDevice(); 111 | 112 | _queue = [_device newCommandQueue]; 113 | } 114 | 115 | - (void)setupLayer { 116 | 117 | _layer = [CAMetalLayer layer]; 118 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 119 | _layer.framebufferOnly = true; 120 | _layer.frame = self.view.bounds; 121 | 122 | CGFloat scale = self.view.contentScaleFactor; 123 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 124 | 125 | [self.view.layer insertSublayer: _layer atIndex: 0]; 126 | } 127 | 128 | - (void)setupRenderTarget { 129 | 130 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 131 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 132 | // colorAttachment.texture = [_layer nextDrawable].texture; 133 | colorAttachment.loadAction = MTLLoadActionClear; 134 | colorAttachment.storeAction = MTLStoreActionStore; 135 | colorAttachment.clearColor = MTLClearColorMake(1, 1, 1, 1); 136 | } 137 | 138 | - (void)setupRenderPipeline { 139 | 140 | id library = [_device newDefaultLibrary]; 141 | 142 | id vertexFunc = [library newFunctionWithName: @"TextureSamplingVertexShader"]; 143 | id fragmentFunc = [library newFunctionWithName: @"TextureSamplingFragmentShader"]; 144 | 145 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 146 | pipelineDescriptor.label = @"Render Pipeline"; 147 | pipelineDescriptor.vertexFunction = vertexFunc; 148 | pipelineDescriptor.fragmentFunction = fragmentFunc; 149 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 150 | 151 | NSError *err; 152 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 153 | 154 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 155 | } 156 | 157 | - (void)loadTexture { 158 | UIImage *image = [UIImage imageNamed: @"avatar.JPG"]; 159 | _texutre = [MetalUtils loadImageTexture: image device: _device usage: MTLTextureUsageShaderRead]; 160 | } 161 | 162 | - (void)setupIndexBuffer { 163 | 164 | _indexBuffer = [_device newBufferWithBytes: indices 165 | length: sizeof(indices) 166 | options: MTLResourceStorageModeShared]; 167 | } 168 | 169 | - (void)setupSamplerState { 170 | MTLSamplerDescriptor *samplerDesc = [MTLSamplerDescriptor new]; 171 | samplerDesc.rAddressMode = [self getModeBySegment: _r_seg]; 172 | samplerDesc.sAddressMode = [self getModeBySegment: _s_seg]; 173 | samplerDesc.tAddressMode = [self getModeBySegment: _t_seg];; 174 | samplerDesc.minFilter = MTLSamplerMinMagFilterNearest; 175 | samplerDesc.magFilter = MTLSamplerMinMagFilterNearest; 176 | samplerDesc.mipFilter = MTLSamplerMipFilterNearest; 177 | 178 | _samplerState = [_device newSamplerStateWithDescriptor: samplerDesc]; 179 | } 180 | 181 | - (void)render { 182 | 183 | id currentDrawable = [_layer nextDrawable]; 184 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 185 | 186 | id commandBuffer = [_queue commandBuffer]; 187 | commandBuffer.label = @"Command Buffer"; 188 | 189 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 190 | encoder.label = @"Render Command Encoder"; 191 | 192 | [encoder setViewport: (MTLViewport) { 193 | .originX = 0, 194 | .originY = 0, 195 | .width = _layer.drawableSize.width, 196 | .height = _layer.drawableSize.height, 197 | .znear = 0, 198 | .zfar = 1 199 | }]; 200 | 201 | [encoder setRenderPipelineState: _renderPipelineState]; 202 | 203 | [encoder setVertexBytes: vertices 204 | length: sizeof(vertices) 205 | atIndex: 0]; 206 | 207 | [encoder setVertexBytes: texCoor 208 | length: sizeof(texCoor) 209 | atIndex: 1]; 210 | 211 | [encoder setFragmentTexture: _texutre 212 | atIndex: 0]; 213 | 214 | [encoder setFragmentSamplerState: _samplerState atIndex: 0]; 215 | 216 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangleStrip 217 | indexCount: 6 218 | indexType: MTLIndexTypeUInt32 219 | indexBuffer: _indexBuffer 220 | indexBufferOffset: 0]; 221 | 222 | [encoder endEncoding]; 223 | 224 | [commandBuffer presentDrawable: currentDrawable]; 225 | 226 | [commandBuffer commit]; 227 | } 228 | 229 | @end 230 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/3. 纹理采样参数详解/TextureSamplingViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 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 | 60 | 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 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/4. Metal 滤镜链/FilterChain.metal: -------------------------------------------------------------------------------- 1 | // 2 | // FilterChain.metal 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/6/4. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | typedef struct 13 | { 14 | float4 position [[position]]; 15 | float2 texCoor; 16 | 17 | } VertexOut; 18 | 19 | constexpr sampler texSampler( 20 | address::clamp_to_edge, 21 | filter::linear 22 | ); 23 | 24 | constant const float3 LUMINANCE_FACTOR = float3(0.2125, 0.7154, 0.0721); 25 | 26 | vertex VertexOut 27 | filterChainVertexShader( 28 | uint vertexID [[ vertex_id ]], 29 | constant float4 *position [[ buffer(0) ]], 30 | constant float2 *texCoor [[ buffer(1) ]] 31 | ) { 32 | VertexOut out; 33 | 34 | out.position = position[vertexID]; 35 | out.texCoor = texCoor[vertexID]; 36 | 37 | return out; 38 | } 39 | 40 | fragment float4 41 | grayFilterFragmentShader( 42 | VertexOut in [[ stage_in ]], 43 | texture2d tex [[ texture(0) ]], 44 | constant float *intensity [[ buffer(0) ]] 45 | ) { 46 | float4 color = tex.sample(texSampler, in.texCoor); 47 | float luminance = dot(color.rgb, LUMINANCE_FACTOR); 48 | float3 mixColor = mix(float3(luminance), color.rgb, 1 - intensity[0]); 49 | return float4(mixColor, 1); 50 | } 51 | 52 | fragment float4 53 | brightnessFilterFragmentShader( 54 | VertexOut in [[ stage_in ]], 55 | texture2d tex [[ texture(0) ]], 56 | constant float *brightness [[ buffer(0) ]] 57 | ) { 58 | float4 color = tex.sample(texSampler, in.texCoor); 59 | return float4(float3(color.rgb + brightness[0]), 1); 60 | } 61 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/4. Metal 滤镜链/FilterChainViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // FilterChainViewController.h 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/6/4. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface FilterChainViewController : UIViewController 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/4. Metal 滤镜链/FilterChainViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // FilterChainViewController.m 3 | // MetalTest 4 | // 5 | // Created by 苏金劲 on 2020/6/4. 6 | // Copyright © 2020 苏金劲. All rights reserved. 7 | // 8 | 9 | #import "FilterChainViewController.h" 10 | 11 | #import "MetalUtils.h" 12 | 13 | static const float grayVertices[] = { 14 | -1, 1, 0, 1, // 左上角 15 | 1, 1, 0, 1, // 右上角 16 | -1, -1, 0, 1, // 左下角 17 | 1, -1, 0, 1, // 右下角 18 | }; 19 | 20 | static const float brightnessVertices[] = { 21 | -0.5, -0.5, 0, 1, // 左下角 22 | 0.5, -0.5, 0, 1, // 右下角 23 | -0.5, 0.5, 0, 1, // 左上角 24 | 0.5, 0.5, 0, 1, // 右上角 25 | }; 26 | 27 | static const float texCoor[] = { 28 | 0, 0, // 左下角 29 | 1, 0, // 右下角 30 | 0, 1, // 左上角 31 | 1, 1, // 右上角 32 | }; 33 | 34 | static const UInt32 indices[] = { 35 | 0, 1, 2, 36 | 1, 3, 2 37 | }; 38 | 39 | @interface FilterChainViewController () 40 | 41 | @property (weak, nonatomic) IBOutlet UIView *renderView; 42 | 43 | @end 44 | 45 | @implementation FilterChainViewController { 46 | id _device; 47 | 48 | id _queue; 49 | 50 | CAMetalLayer *_layer; 51 | 52 | MTLRenderPassDescriptor *_grayRenderTargetDesc; 53 | MTLRenderPassDescriptor *_BrightnessRenderTargetDesc; 54 | 55 | id _grayRenderPipelineState; 56 | id _brightnessRenderPipelineState; 57 | 58 | id _sourceTexutre; 59 | id _grayResultTexutre; 60 | 61 | id _indexBuffer; 62 | 63 | float _grayIntensity, _brightness; 64 | } 65 | 66 | #pragma mark - Life Cycle 67 | 68 | - (void)viewDidLoad { 69 | [super viewDidLoad]; 70 | 71 | _grayIntensity = 0; 72 | _brightness = 0; 73 | } 74 | 75 | - (void)viewDidAppear:(BOOL)animated { 76 | [super viewDidAppear: animated]; 77 | 78 | [self setupMetal]; 79 | [self setupLayer]; 80 | [self setupRenderTarget]; 81 | [self setupRenderPipeline]; 82 | [self loadTexture]; 83 | [self setupIndexBuffer]; 84 | 85 | [self render]; 86 | // [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 87 | } 88 | 89 | #pragma mark - Response 90 | 91 | - (IBAction)onGraySliderChanged:(id)sender { 92 | _grayIntensity = ((UISlider *)sender).value; 93 | [self render]; 94 | } 95 | 96 | - (IBAction)onBrightnessSliderChanged:(id)sender { 97 | _brightness = ((UISlider *)sender).value; 98 | [self render]; 99 | } 100 | 101 | #pragma mark - Metal 102 | 103 | - (void)setupMetal { 104 | 105 | _device = MTLCreateSystemDefaultDevice(); 106 | 107 | _queue = [_device newCommandQueue]; 108 | } 109 | 110 | - (void)setupLayer { 111 | 112 | _layer = [CAMetalLayer layer]; 113 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 114 | _layer.framebufferOnly = true; 115 | _layer.frame = self.renderView.bounds; 116 | 117 | CGFloat scale = self.renderView.contentScaleFactor; 118 | _layer.drawableSize = CGSizeApplyAffineTransform(self.renderView.bounds.size, CGAffineTransformMakeScale(scale, scale)); 119 | 120 | [self.renderView.layer insertSublayer: _layer atIndex: 0]; 121 | } 122 | 123 | - (void)setupRenderTarget { 124 | 125 | _grayRenderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 126 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _grayRenderTargetDesc.colorAttachments[0]; 127 | // colorAttachment.texture = [_layer nextDrawable].texture; 128 | colorAttachment.loadAction = MTLLoadActionClear; 129 | colorAttachment.storeAction = MTLStoreActionStore; 130 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 131 | 132 | _BrightnessRenderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 133 | colorAttachment = _BrightnessRenderTargetDesc.colorAttachments[0]; 134 | // colorAttachment.texture = [_layer nextDrawable].texture; 135 | colorAttachment.loadAction = MTLLoadActionClear; 136 | colorAttachment.storeAction = MTLStoreActionStore; 137 | colorAttachment.clearColor = MTLClearColorMake(1, 1, 1, 1); 138 | } 139 | 140 | - (void)setupRenderPipeline { 141 | 142 | id library = [_device newDefaultLibrary]; 143 | 144 | id vertexFunc = [library newFunctionWithName: @"filterChainVertexShader"]; 145 | id grayfragmentFunc = [library newFunctionWithName: @"grayFilterFragmentShader"]; 146 | id brightnessfragmentFunc = [library newFunctionWithName: @"brightnessFilterFragmentShader"]; 147 | 148 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 149 | NSError *err; 150 | 151 | // Gray pipeline desc 152 | pipelineDescriptor.label = @"Gray Render Pipeline"; 153 | pipelineDescriptor.vertexFunction = vertexFunc; 154 | pipelineDescriptor.fragmentFunction = grayfragmentFunc; 155 | pipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm; 156 | 157 | _grayRenderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 158 | NSAssert(_grayRenderPipelineState != nil, @"Failed to create pipeline state: %@", err); 159 | 160 | // Brightness pipeline desc 161 | pipelineDescriptor.label = @"Brightness Render Pipeline"; 162 | pipelineDescriptor.vertexFunction = vertexFunc; 163 | pipelineDescriptor.fragmentFunction = brightnessfragmentFunc; 164 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 165 | 166 | _brightnessRenderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 167 | NSAssert(_grayRenderPipelineState != nil, @"Failed to create pipeline state: %@", err); 168 | } 169 | 170 | - (void)loadTexture { 171 | UIImage *image = [UIImage imageNamed: @"avatar.JPG"]; 172 | _sourceTexutre = [MetalUtils loadImageTexture: image 173 | device: _device 174 | usage: MTLTextureUsageShaderRead]; 175 | 176 | CGImageRef cgImage = image.CGImage; 177 | _grayResultTexutre = [MetalUtils createEmptyTexture: _device 178 | WithWidth: CGImageGetWidth(cgImage) 179 | withHeight: CGImageGetHeight(cgImage) 180 | usage: MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget]; 181 | } 182 | 183 | - (void)setupIndexBuffer { 184 | 185 | _indexBuffer = [_device newBufferWithBytes: indices 186 | length: sizeof(indices) 187 | options: MTLResourceStorageModeShared]; 188 | } 189 | 190 | - (void)render_gray { 191 | 192 | // 将本次 Command Encoder 和 渲染的目标(MTLTexture)关联起来 193 | _grayRenderTargetDesc.colorAttachments[0].texture = _grayResultTexutre; 194 | 195 | id commandBuffer = [_queue commandBuffer]; 196 | commandBuffer.label = @"Gray Command Buffer"; 197 | 198 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _grayRenderTargetDesc]; 199 | encoder.label = @"Gray Command Encoder"; 200 | 201 | [encoder setViewport: (MTLViewport) { 202 | .originX = 0, 203 | .originY = 0, 204 | .width = _sourceTexutre.width, 205 | .height = _sourceTexutre.height, 206 | .znear = 0, 207 | .zfar = 1 208 | }]; 209 | 210 | [encoder setRenderPipelineState: _grayRenderPipelineState]; 211 | 212 | [encoder setVertexBytes: grayVertices 213 | length: sizeof(grayVertices) 214 | atIndex: 0]; 215 | 216 | [encoder setVertexBytes: texCoor 217 | length: sizeof(texCoor) 218 | atIndex: 1]; 219 | 220 | [encoder setFragmentTexture: _sourceTexutre 221 | atIndex: 0]; 222 | 223 | [encoder setFragmentBytes: &_grayIntensity 224 | length: sizeof(float) 225 | atIndex: 0]; 226 | 227 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangleStrip 228 | indexCount: 6 229 | indexType: MTLIndexTypeUInt32 230 | indexBuffer: _indexBuffer 231 | indexBufferOffset: 0]; 232 | 233 | [encoder endEncoding]; 234 | 235 | [commandBuffer commit]; 236 | } 237 | 238 | - (void)render_brightness { 239 | 240 | id currentDrawable = [_layer nextDrawable]; 241 | 242 | // 将本次 Command Encoder 和 渲染的目标(Layer 的 texture)关联起来 243 | _BrightnessRenderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 244 | 245 | id commandBuffer = [_queue commandBuffer]; 246 | commandBuffer.label = @"Brightness Command Buffer"; 247 | 248 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _BrightnessRenderTargetDesc]; 249 | encoder.label = @"Brightness Command Encoder"; 250 | 251 | [encoder setViewport: (MTLViewport) { 252 | .originX = 0, 253 | .originY = 0, 254 | .width = _layer.drawableSize.width, 255 | .height = _layer.drawableSize.height, 256 | .znear = 0, 257 | .zfar = 1 258 | }]; 259 | 260 | [encoder setRenderPipelineState: _brightnessRenderPipelineState]; 261 | 262 | [encoder setVertexBytes: brightnessVertices 263 | length: sizeof(brightnessVertices) 264 | atIndex: 0]; 265 | 266 | [encoder setVertexBytes: texCoor 267 | length: sizeof(texCoor) 268 | atIndex: 1]; 269 | 270 | [encoder setFragmentTexture: _grayResultTexutre 271 | atIndex: 0]; 272 | 273 | [encoder setFragmentBytes: &_brightness 274 | length: sizeof(float) 275 | atIndex: 0]; 276 | 277 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangleStrip 278 | indexCount: 6 279 | indexType: MTLIndexTypeUInt32 280 | indexBuffer: _indexBuffer 281 | indexBufferOffset: 0]; 282 | 283 | [encoder endEncoding]; 284 | 285 | [commandBuffer presentDrawable: currentDrawable]; 286 | 287 | [commandBuffer commit]; 288 | } 289 | 290 | - (void)render { 291 | /* 292 | 1. 先进行灰度的渲染(输入是原图(sourceImage),输出是空白的 MTLTexture) 293 | 294 | 2. 将第一步输出的 MTLTexture 作为 Brightness 的输入,输出到 Layer 的 texture 295 | 296 | */ 297 | [self render_gray]; 298 | [self render_brightness]; 299 | } 300 | 301 | @end 302 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/4. Metal 滤镜链/FilterChainViewController.xib: -------------------------------------------------------------------------------- 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 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 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 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/5. 三维变换/ThreeDimentionsTransform.metal: -------------------------------------------------------------------------------- 1 | //// ThreeDimentionsTransform.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/8. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | typedef struct 12 | { 13 | float4 position [[position]]; 14 | float2 texCoor; 15 | 16 | } VertexOut; 17 | 18 | vertex VertexOut 19 | ThreeDimentionTransformVertexShader( 20 | uint vertexID [[ vertex_id ]], 21 | constant float4 *position [[ buffer(0) ]], 22 | constant float2 *texCoor [[ buffer(1) ]], 23 | constant float4x4 *modelMatrix [[ buffer(2) ]], 24 | constant float4x4 *viewMatrix [[ buffer(3) ]], 25 | constant float4x4 *projectionMatrix [[ buffer(4) ]] 26 | ) { 27 | VertexOut out; 28 | 29 | out.position = projectionMatrix[0] * viewMatrix[0] * modelMatrix[0] * position[vertexID]; 30 | out.texCoor = texCoor[vertexID]; 31 | 32 | return out; 33 | } 34 | 35 | fragment float4 36 | ThreeDimentionTransformFragmentShader( 37 | VertexOut in [[ stage_in ]], 38 | texture2d tex [[ texture(0) ]] 39 | ) { 40 | constexpr sampler texSampler; 41 | float4 color = tex.sample(texSampler, in.texCoor); 42 | return color; 43 | } 44 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/5. 三维变换/ThreeDimentionsTransformViewController.h: -------------------------------------------------------------------------------- 1 | //// ThreeDimentionsTransformViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/8. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface ThreeDimentionsTransformViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/5. 三维变换/ThreeDimentionsTransformViewController.m: -------------------------------------------------------------------------------- 1 | //// ThreeDimentionsTransformViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/8. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "ThreeDimentionsTransformViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | #import "MetalMatrix.h" 13 | 14 | static const float vertices[] = { 15 | -0.5, -0.5, 0, 1, // 左下角 16 | 0.5, -0.5, 0, 1, // 右下角 17 | -0.5, 0.5, 0, 1, // 左上角 18 | 0.5, 0.5, 0, 1, // 右上角 19 | }; 20 | 21 | static const float texCoor[] = { 22 | 0, 0, // 左下角 23 | 1, 0, // 右下角 24 | 0, 1, // 左上角 25 | 1, 1, // 右上角 26 | }; 27 | 28 | static const UInt32 indices[] = { 29 | 0, 1, 2, 30 | 1, 3, 2 31 | }; 32 | 33 | @interface ThreeDimentionsTransformViewController () 34 | 35 | @end 36 | 37 | @implementation ThreeDimentionsTransformViewController { 38 | 39 | id _device; 40 | 41 | id _queue; 42 | 43 | CAMetalLayer *_layer; 44 | 45 | MTLRenderPassDescriptor *_renderTargetDesc; 46 | 47 | id _renderPipelineState; 48 | 49 | id _texutre; 50 | 51 | id _indexBuffer; 52 | 53 | simd_float4x4 _modelMatrix, _viewMatrix, _projectionMatrix; 54 | 55 | CADisplayLink *_dis; 56 | float _cameraDistance; 57 | int _cameraDegree; 58 | } 59 | 60 | #pragma mark - Life Circle 61 | 62 | - (void)viewDidLoad { 63 | [super viewDidLoad]; 64 | 65 | _cameraDistance = 2; 66 | 67 | _dis = [CADisplayLink displayLinkWithTarget: self selector: @selector(rotateCamera)]; 68 | } 69 | 70 | - (void)viewDidAppear:(BOOL)animated { 71 | [super viewDidAppear: animated]; 72 | 73 | [self setupMetal]; 74 | [self setupLayer]; 75 | [self setupRenderTarget]; 76 | [self setupRenderPipeline]; 77 | [self loadTexture]; 78 | [self setupIndexBuffer]; 79 | 80 | _modelMatrix = [MetalMatrix mm_identity]; 81 | 82 | _viewMatrix = [MetalMatrix mm_lookAtWithEyeX: 0 83 | withEyeY: 0 84 | withEyeZ: -_cameraDistance 85 | withCenterX: 0 86 | withCenterY: 0 87 | withCenterZ: 0 88 | withUpX: 0 89 | withUpY: 1 90 | withUpZ: 0]; 91 | 92 | _projectionMatrix = [MetalMatrix mm_perspectiveWithFovy: 90 93 | withWidth: CGRectGetWidth(_layer.bounds) 94 | withHeight: CGRectGetHeight(_layer.bounds) 95 | withNear: 0.1 96 | withFar: 100]; 97 | 98 | [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 99 | } 100 | 101 | #pragma mark - Rotate Camera 102 | 103 | - (void)rotateCamera { 104 | 105 | float x = _cameraDistance * sin(MM_RADIANS(_cameraDegree)); 106 | float z = _cameraDistance * cos(MM_RADIANS(_cameraDegree)); 107 | 108 | _viewMatrix = [MetalMatrix mm_lookAtWithEyeX: x 109 | withEyeY: 0 110 | withEyeZ: -z 111 | withCenterX: 0 112 | withCenterY: 0 113 | withCenterZ: 0 114 | withUpX: 0 115 | withUpY: 1 116 | withUpZ: 0]; 117 | [self render]; 118 | 119 | if (_cameraDegree == 360) { 120 | _cameraDegree = 0; 121 | } else { 122 | _cameraDegree += 3; 123 | } 124 | } 125 | 126 | #pragma mark - Metal 127 | 128 | - (void)setupMetal { 129 | 130 | _device = MTLCreateSystemDefaultDevice(); 131 | 132 | _queue = [_device newCommandQueue]; 133 | } 134 | 135 | - (void)setupLayer { 136 | 137 | _layer = [CAMetalLayer layer]; 138 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 139 | _layer.framebufferOnly = true; 140 | _layer.frame = self.view.bounds; 141 | 142 | CGFloat scale = self.view.contentScaleFactor; 143 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 144 | 145 | [self.view.layer insertSublayer: _layer atIndex: 0]; 146 | } 147 | 148 | - (void)setupRenderTarget { 149 | 150 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 151 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 152 | // colorAttachment.texture = [_layer nextDrawable].texture; 153 | colorAttachment.loadAction = MTLLoadActionClear; 154 | colorAttachment.storeAction = MTLStoreActionStore; 155 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 156 | } 157 | 158 | - (void)setupRenderPipeline { 159 | 160 | id library = [_device newDefaultLibrary]; 161 | 162 | id vertexFunc = [library newFunctionWithName: @"ThreeDimentionTransformVertexShader"]; 163 | id fragmentFunc = [library newFunctionWithName: @"ThreeDimentionTransformFragmentShader"]; 164 | 165 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 166 | pipelineDescriptor.label = @"Render Pipeline"; 167 | pipelineDescriptor.vertexFunction = vertexFunc; 168 | pipelineDescriptor.fragmentFunction = fragmentFunc; 169 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 170 | 171 | NSError *err; 172 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 173 | 174 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 175 | } 176 | 177 | - (void)loadTexture { 178 | UIImage *image = [UIImage imageNamed: @"avatar.JPG"]; 179 | _texutre = [MetalUtils loadImageTexture: image device: _device usage: MTLTextureUsageShaderRead]; 180 | } 181 | 182 | - (void)setupIndexBuffer { 183 | 184 | _indexBuffer = [_device newBufferWithBytes: indices 185 | length: sizeof(indices) 186 | options: MTLResourceStorageModeShared]; 187 | } 188 | 189 | - (void)render { 190 | 191 | id currentDrawable = [_layer nextDrawable]; 192 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 193 | 194 | id commandBuffer = [_queue commandBuffer]; 195 | commandBuffer.label = @"Command Buffer"; 196 | 197 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 198 | encoder.label = @"Render Command Encoder"; 199 | 200 | [encoder setViewport: (MTLViewport) { 201 | .originX = 0, 202 | .originY = 0, 203 | .width = _layer.drawableSize.width, 204 | .height = _layer.drawableSize.height, 205 | .znear = 0, 206 | .zfar = 1 207 | }]; 208 | 209 | [encoder setRenderPipelineState: _renderPipelineState]; 210 | 211 | [encoder setVertexBytes: vertices 212 | length: sizeof(vertices) 213 | atIndex: 0]; 214 | 215 | [encoder setVertexBytes: texCoor 216 | length: sizeof(texCoor) 217 | atIndex: 1]; 218 | 219 | [encoder setVertexBytes: &_modelMatrix 220 | length: [MetalMatrix mm_matrixSize] 221 | atIndex: 2]; 222 | 223 | [encoder setVertexBytes: &_viewMatrix 224 | length: [MetalMatrix mm_matrixSize] 225 | atIndex: 3]; 226 | 227 | [encoder setVertexBytes: &_projectionMatrix 228 | length: [MetalMatrix mm_matrixSize] 229 | atIndex: 4]; 230 | 231 | [encoder setFragmentTexture: _texutre 232 | atIndex: 0]; 233 | 234 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangleStrip 235 | indexCount: 6 236 | indexType: MTLIndexTypeUInt32 237 | indexBuffer: _indexBuffer 238 | indexBufferOffset: 0]; 239 | 240 | [encoder endEncoding]; 241 | 242 | [commandBuffer presentDrawable: currentDrawable]; 243 | 244 | [commandBuffer commit]; 245 | } 246 | 247 | @end 248 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/5. 三维变换/ThreeDimentionsTransformViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/6. 旋转的立方体/RotatingCube.metal: -------------------------------------------------------------------------------- 1 | //// RotatingCube.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/8. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | typedef struct 12 | { 13 | float4 position [[position]]; 14 | float4 vertexColor; 15 | 16 | } VertexOut; 17 | 18 | vertex VertexOut 19 | RotatingCubeVertexShader( 20 | uint vertexID [[ vertex_id ]], 21 | constant float4 *position [[ buffer(0) ]], 22 | constant float4 *vertexColor [[ buffer(1) ]], 23 | constant float4x4 *modelMatrix [[ buffer(2) ]], 24 | constant float4x4 *viewMatrix [[ buffer(3) ]], 25 | constant float4x4 *projectionMatrix [[ buffer(4) ]] 26 | ) { 27 | VertexOut out; 28 | 29 | out.position = projectionMatrix[0] * viewMatrix[0] * modelMatrix[0] * position[vertexID]; 30 | out.vertexColor = vertexColor[vertexID]; 31 | 32 | return out; 33 | } 34 | 35 | fragment float4 36 | RotatingCubeFragmentShader( 37 | VertexOut in [[ stage_in ]] 38 | ) { 39 | return in.vertexColor; 40 | } 41 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/6. 旋转的立方体/RotatingCubeViewController.h: -------------------------------------------------------------------------------- 1 | //// RotatingCubeViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/8. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface RotatingCubeViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/6. 旋转的立方体/RotatingCubeViewController.m: -------------------------------------------------------------------------------- 1 | //// RotatingCubeViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/8. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "RotatingCubeViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | #import "MetalMatrix.h" 13 | 14 | static const float vertices[] = { 15 | -1, 1, 1, 1, // 0 16 | 1, 1, 1, 1, // 1 17 | 1, -1, 1, 1, // 2 18 | -1, -1, 1, 1, // 3 19 | -1, 1, -1, 1, // 4 20 | 1, 1, -1, 1, // 5 21 | 1, -1, -1, 1, // 6 22 | -1, -1, -1, 1 // 7 23 | }; 24 | 25 | static const float vertexColor[] = { 26 | 1, 0, 0, 1, // 0 27 | 1, 1, 0, 1, // 1 28 | 1, 1, 1, 1, // 2 29 | 1, 0, 1, 1, // 3 30 | 0, 1, 0, 1, // 4 31 | 0, 1, 1, 1, // 5 32 | 0, 0, 1, 1, // 6 33 | 0, 0, 0, 1, // 7 34 | }; 35 | 36 | static const UInt32 indices[] = { 37 | // 正面 38 | 0, 1, 3, 39 | 1, 2, 3, 40 | 41 | // 右面 42 | 1, 2, 6, 43 | 1, 5, 6, 44 | 45 | // 背面 46 | 4, 5, 7, 47 | 5, 6, 7, 48 | 49 | // 左面 50 | 3, 4, 7, 51 | 0, 3, 4, 52 | 53 | // 上面 54 | 0, 1, 4, 55 | 1, 4, 5, 56 | 57 | // 下面 58 | 2, 3, 6, 59 | 3, 6, 7, 60 | }; 61 | 62 | 63 | @interface RotatingCubeViewController () 64 | 65 | @end 66 | 67 | @implementation RotatingCubeViewController { 68 | 69 | id _device; 70 | 71 | id _queue; 72 | 73 | CAMetalLayer *_layer; 74 | 75 | MTLRenderPassDescriptor *_renderTargetDesc; 76 | 77 | id _renderPipelineState; 78 | 79 | id _depthTexture; 80 | 81 | id _depthStencilState; 82 | 83 | id _indexBuffer; 84 | 85 | simd_float4x4 _modelMatrix, _viewMatrix, _projectionMatrix; 86 | 87 | CADisplayLink *_dis; 88 | float _cameraDistance; 89 | } 90 | 91 | #pragma mark - Life Circle 92 | 93 | - (void)viewDidLoad { 94 | [super viewDidLoad]; 95 | 96 | _cameraDistance = 10; 97 | 98 | _dis = [CADisplayLink displayLinkWithTarget: self selector: @selector(rotateCube)]; 99 | } 100 | 101 | - (void)viewDidAppear:(BOOL)animated { 102 | [super viewDidAppear: animated]; 103 | 104 | [self setupMetal]; 105 | [self setupLayer]; 106 | [self setupRenderTarget]; 107 | [self setupRenderPipeline]; 108 | [self setupDepthStencilTexuture]; 109 | [self setupDepthStencil]; 110 | [self setupIndexBuffer]; 111 | 112 | _modelMatrix = [MetalMatrix mm_identity]; 113 | 114 | _viewMatrix = [MetalMatrix mm_lookAtWithEyeX: 0 115 | withEyeY: 0 116 | withEyeZ: -_cameraDistance 117 | withCenterX: 0 118 | withCenterY: 0 119 | withCenterZ: 0 120 | withUpX: 0 121 | withUpY: 1 122 | withUpZ: 0]; 123 | 124 | _projectionMatrix = [MetalMatrix mm_perspectiveWithFovy: 90 125 | withWidth: CGRectGetWidth(_layer.bounds) 126 | withHeight: CGRectGetHeight(_layer.bounds) 127 | withNear: 0.1 128 | withFar: 100]; 129 | 130 | // [self render]; 131 | [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 132 | } 133 | 134 | #pragma mark - Rotate Cube 135 | 136 | - (void)rotateCube { 137 | double time = [[NSDate date] timeIntervalSince1970]; 138 | float angle = sin(time); 139 | _modelMatrix = [MetalMatrix mm_rotate: angle * 360 withX: 1 withY: 1 withZ: 1]; 140 | [self render]; 141 | } 142 | 143 | #pragma mark - Metal 144 | 145 | - (void)setupMetal { 146 | 147 | _device = MTLCreateSystemDefaultDevice(); 148 | 149 | _queue = [_device newCommandQueue]; 150 | } 151 | 152 | - (void)setupLayer { 153 | 154 | _layer = [CAMetalLayer layer]; 155 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 156 | _layer.framebufferOnly = true; 157 | _layer.frame = self.view.bounds; 158 | 159 | CGFloat scale = self.view.contentScaleFactor; 160 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 161 | 162 | [self.view.layer insertSublayer: _layer atIndex: 0]; 163 | } 164 | 165 | - (void)setupRenderTarget { 166 | 167 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 168 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 169 | // colorAttachment.texture = [_layer nextDrawable].texture; 170 | colorAttachment.loadAction = MTLLoadActionClear; 171 | colorAttachment.storeAction = MTLStoreActionStore; 172 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 173 | 174 | _renderTargetDesc.depthAttachment.clearDepth = 1; 175 | } 176 | 177 | - (void)setupRenderPipeline { 178 | 179 | id library = [_device newDefaultLibrary]; 180 | 181 | id vertexFunc = [library newFunctionWithName: @"RotatingCubeVertexShader"]; 182 | id fragmentFunc = [library newFunctionWithName: @"RotatingCubeFragmentShader"]; 183 | 184 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 185 | pipelineDescriptor.label = @"Render Pipeline"; 186 | pipelineDescriptor.vertexFunction = vertexFunc; 187 | pipelineDescriptor.fragmentFunction = fragmentFunc; 188 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 189 | 190 | pipelineDescriptor.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float; // depth format 191 | 192 | NSError *err; 193 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 194 | 195 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 196 | } 197 | 198 | - (void)setupDepthStencilTexuture { 199 | 200 | _depthTexture = [MetalUtils createDepthStencilTexture: _device 201 | WithWidth: _layer.drawableSize.width 202 | withHeight: _layer.drawableSize.height]; 203 | 204 | _renderTargetDesc.depthAttachment.texture = _depthTexture; 205 | } 206 | 207 | - (void)setupDepthStencil { 208 | MTLDepthStencilDescriptor *depthStencilDesc = [MTLDepthStencilDescriptor new]; 209 | depthStencilDesc.depthCompareFunction = MTLCompareFunctionLess; 210 | depthStencilDesc.depthWriteEnabled = true; 211 | _depthStencilState = [_device newDepthStencilStateWithDescriptor: depthStencilDesc]; 212 | } 213 | 214 | - (void)setupIndexBuffer { 215 | 216 | _indexBuffer = [_device newBufferWithBytes: indices 217 | length: sizeof(indices) 218 | options: MTLResourceStorageModeShared]; 219 | } 220 | 221 | - (void)render { 222 | 223 | id currentDrawable = [_layer nextDrawable]; 224 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 225 | 226 | id commandBuffer = [_queue commandBuffer]; 227 | commandBuffer.label = @"Command Buffer"; 228 | 229 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 230 | encoder.label = @"Render Command Encoder"; 231 | 232 | [encoder setViewport: (MTLViewport) { 233 | .originX = 0, 234 | .originY = 0, 235 | .width = _layer.drawableSize.width, 236 | .height = _layer.drawableSize.height, 237 | .znear = 0, 238 | .zfar = 1 239 | }]; 240 | 241 | [encoder setRenderPipelineState: _renderPipelineState]; 242 | 243 | // depth 244 | [encoder setDepthStencilState: _depthStencilState]; 245 | 246 | [encoder setVertexBytes: vertices 247 | length: sizeof(vertices) 248 | atIndex: 0]; 249 | 250 | [encoder setVertexBytes: vertexColor 251 | length: sizeof(vertexColor) 252 | atIndex: 1]; 253 | 254 | [encoder setVertexBytes: &_modelMatrix 255 | length: [MetalMatrix mm_matrixSize] 256 | atIndex: 2]; 257 | 258 | [encoder setVertexBytes: &_viewMatrix 259 | length: [MetalMatrix mm_matrixSize] 260 | atIndex: 3]; 261 | 262 | [encoder setVertexBytes: &_projectionMatrix 263 | length: [MetalMatrix mm_matrixSize] 264 | atIndex: 4]; 265 | 266 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangle // Triangle 267 | indexCount: sizeof(indices) / sizeof(UInt32) 268 | indexType: MTLIndexTypeUInt32 269 | indexBuffer: _indexBuffer 270 | indexBufferOffset: 0]; 271 | 272 | [encoder endEncoding]; 273 | 274 | [commandBuffer presentDrawable: currentDrawable]; 275 | 276 | [commandBuffer commit]; 277 | } 278 | 279 | @end 280 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/6. 旋转的立方体/RotatingCubeViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/7. 渲染摄像头采集的数据(CVMetalTextureCache)/RenderCameraBGRA.metal: -------------------------------------------------------------------------------- 1 | //// RenderCameraBGRA.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/9. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | constexpr sampler texSampler; 12 | 13 | typedef struct 14 | { 15 | float4 position [[position]]; 16 | float2 texCoor; 17 | 18 | } VertexOut; 19 | 20 | vertex VertexOut 21 | RenderCameraBGRAVertexShader( 22 | uint vertexID [[ vertex_id ]], 23 | constant float4 *position [[ buffer(0) ]], 24 | constant float2 *texCoor [[ buffer(1) ]] 25 | ) { 26 | VertexOut out; 27 | 28 | out.position = position[vertexID]; 29 | out.texCoor = texCoor[vertexID]; 30 | 31 | return out; 32 | } 33 | 34 | fragment float4 35 | RenderCameraBGRAFragmentShader( 36 | VertexOut in [[ stage_in ]], 37 | texture2d tex [[ texture(0) ]] 38 | ) { 39 | float4 color = tex.sample(texSampler, in.texCoor); 40 | return color; 41 | } 42 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/7. 渲染摄像头采集的数据(CVMetalTextureCache)/RenderCameraBGRAViewController.h: -------------------------------------------------------------------------------- 1 | //// RenderCameraBGRAViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/9. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface RenderCameraBGRAViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/7. 渲染摄像头采集的数据(CVMetalTextureCache)/RenderCameraBGRAViewController.m: -------------------------------------------------------------------------------- 1 | //// RenderCameraBGRAViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/9. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "RenderCameraBGRAViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | #import 13 | 14 | static const float vertices[] = { 15 | -0.5, -0.5, 0, 1, // 左下角 16 | 0.5, -0.5, 0, 1, // 右下角 17 | -0.5, 0.5, 0, 1, // 左上角 18 | 0.5, 0.5, 0, 1, // 右上角 19 | }; 20 | 21 | static const float texCoor[] = { 22 | 0, 0, // 左下角 23 | 1, 0, // 右下角 24 | 0, 1, // 左上角 25 | 1, 1, // 右上角 26 | }; 27 | 28 | static const UInt32 indices[] = { 29 | 0, 1, 2, 30 | 1, 3, 2 31 | }; 32 | 33 | @interface RenderCameraBGRAViewController () 34 | 35 | @end 36 | 37 | @implementation RenderCameraBGRAViewController { 38 | 39 | // AVFoundation 40 | AVCaptureSession *_session; 41 | dispatch_queue_t _captureQueue; 42 | 43 | // [Core Graphics - Metal] Texture Cache 44 | CVMetalTextureCacheRef _textureCache; 45 | 46 | // Metal 47 | id _device; 48 | 49 | id _queue; 50 | 51 | CAMetalLayer *_layer; 52 | 53 | MTLRenderPassDescriptor *_renderTargetDesc; 54 | 55 | id _renderPipelineState; 56 | 57 | id _texutre; 58 | 59 | id _indexBuffer; 60 | } 61 | 62 | #pragma mark - Life Circle 63 | 64 | - (void)viewDidAppear:(BOOL)animated { 65 | [super viewDidAppear: animated]; 66 | 67 | [self setupCamera: kCVPixelFormatType_32BGRA]; 68 | 69 | [self setupMetal]; 70 | [self setupLayer]; 71 | [self setupRenderTarget]; 72 | [self setupRenderPipeline]; 73 | [self setupIndexBuffer]; 74 | [self setupTextureCache]; 75 | 76 | [AVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo 77 | completionHandler:^(BOOL granted) { 78 | if (granted) { 79 | [self->_session startRunning]; 80 | } 81 | }]; 82 | } 83 | 84 | #pragma mark - 采集的 Pixel Buffer 转换成 Metal 的 Texture 85 | 86 | - (CVMetalTextureRef)acquireTextureFromBuffer: (CVPixelBufferRef)buffer { 87 | 88 | CVMetalTextureRef texture; 89 | CVReturn ret = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, 90 | _textureCache, 91 | buffer, 92 | NULL, 93 | MTLPixelFormatBGRA8Unorm, 94 | CVPixelBufferGetWidth(buffer), 95 | CVPixelBufferGetHeight(buffer), 96 | 0, 97 | &texture); 98 | 99 | if (ret != kCVReturnSuccess) { 100 | NSLog(@"Read texture faild from sample buffer, %d", ret); 101 | } 102 | 103 | return texture; 104 | } 105 | 106 | #pragma mark - Camera 107 | 108 | - (bool)setupCamera: (OSType)pixelFormatType { 109 | 110 | _session = [[AVCaptureSession alloc] init]; 111 | _captureQueue = dispatch_queue_create(0, 0); 112 | 113 | AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo]; 114 | device = [AVCaptureDevice defaultDeviceWithDeviceType: AVCaptureDeviceTypeBuiltInWideAngleCamera 115 | mediaType: AVMediaTypeVideo 116 | position: AVCaptureDevicePositionBack]; 117 | 118 | if (device == nil) { 119 | return false; 120 | } 121 | 122 | NSError *err; 123 | AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device error: &err]; 124 | 125 | if (input == nil || err != nil) { 126 | return false; 127 | } 128 | 129 | [_session beginConfiguration]; 130 | _session.sessionPreset = AVCaptureSessionPreset640x480; 131 | 132 | if (![_session canAddInput: input]) { 133 | [_session commitConfiguration]; 134 | return false; 135 | } 136 | [_session addInput: input]; 137 | 138 | AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; 139 | output.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey: @(pixelFormatType) }; 140 | [output setAlwaysDiscardsLateVideoFrames: true]; 141 | [output setSampleBufferDelegate: self queue: _captureQueue]; 142 | 143 | if (![_session canAddOutput: output]) { 144 | [_session commitConfiguration]; 145 | return false; 146 | } 147 | [_session addOutput: output]; 148 | 149 | AVCaptureConnection *connection = [output connectionWithMediaType: AVMediaTypeVideo]; 150 | 151 | if (connection == nil) { 152 | [_session commitConfiguration]; 153 | return false; 154 | } 155 | 156 | // 因为 Metal 的纹理 Y 轴和 UIKit 的是相反的,所以这里采集需要上下颠倒 157 | connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 158 | // [connection setVideoMirrored: true]; 159 | 160 | [_session commitConfiguration]; 161 | return true; 162 | } 163 | 164 | - (void)captureOutput:(AVCaptureOutput *)output 165 | didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 166 | fromConnection:(AVCaptureConnection *)connection { 167 | 168 | // Metal 是线程安全的,其他线程的返回没有关系 169 | 170 | CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 171 | 172 | CVPixelBufferLockBaseAddress(imageBuffer, 0); 173 | 174 | CVMetalTextureRef texture = [self acquireTextureFromBuffer: imageBuffer]; // PixelBuffer => CV Metal Texture 175 | _texutre = CVMetalTextureGetTexture(texture); // CV Metal Texture -> MTLTexture 176 | 177 | [self render]; 178 | 179 | CVMetalTextureCacheFlush(_textureCache, 0); // 渲染完毕之后清空一下 texture cache 180 | 181 | CVPixelBufferUnlockBaseAddress(imageBuffer, 0); 182 | 183 | if (texture != NULL) { // 如果 texture 为 NULL,再 Release 就会出现 `EXC_BREAKPOINT` crash! 184 | CFRelease(texture); // 没有这个,就会不再采集!!!! 185 | } 186 | } 187 | 188 | #pragma mark - Metal 189 | 190 | - (void)setupMetal { 191 | 192 | _device = MTLCreateSystemDefaultDevice(); 193 | 194 | _queue = [_device newCommandQueue]; 195 | } 196 | 197 | - (void)setupLayer { 198 | 199 | _layer = [CAMetalLayer layer]; 200 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 201 | _layer.framebufferOnly = true; 202 | _layer.frame = self.view.bounds; 203 | 204 | CGFloat scale = self.view.contentScaleFactor; 205 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 206 | 207 | [self.view.layer insertSublayer: _layer atIndex: 0]; 208 | } 209 | 210 | - (void)setupRenderTarget { 211 | 212 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 213 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 214 | // colorAttachment.texture = [_layer nextDrawable].texture; 215 | colorAttachment.loadAction = MTLLoadActionClear; 216 | colorAttachment.storeAction = MTLStoreActionStore; 217 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 218 | } 219 | 220 | - (void)setupRenderPipeline { 221 | 222 | id library = [_device newDefaultLibrary]; 223 | 224 | id vertexFunc = [library newFunctionWithName: @"RenderCameraBGRAVertexShader"]; 225 | id fragmentFunc = [library newFunctionWithName: @"RenderCameraBGRAFragmentShader"]; 226 | 227 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 228 | pipelineDescriptor.label = @"Render Pipeline"; 229 | pipelineDescriptor.vertexFunction = vertexFunc; 230 | pipelineDescriptor.fragmentFunction = fragmentFunc; 231 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 232 | 233 | NSError *err; 234 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 235 | 236 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 237 | } 238 | 239 | - (void)setupIndexBuffer { 240 | 241 | _indexBuffer = [_device newBufferWithBytes: indices 242 | length: sizeof(indices) 243 | options: MTLResourceStorageModeShared]; 244 | } 245 | 246 | - (void)setupTextureCache { 247 | 248 | CVReturn ret = CVMetalTextureCacheCreate(NULL, NULL, _device, NULL, &_textureCache); 249 | 250 | if (ret != kCVReturnSuccess) { 251 | NSLog(@"Create cache failed"); 252 | } 253 | } 254 | 255 | - (void)render { 256 | 257 | id currentDrawable = [_layer nextDrawable]; 258 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 259 | 260 | id commandBuffer = [_queue commandBuffer]; 261 | commandBuffer.label = @"Command Buffer"; 262 | 263 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 264 | encoder.label = @"Render Command Encoder"; 265 | 266 | [encoder setViewport: (MTLViewport) { 267 | .originX = 0, 268 | .originY = 0, 269 | .width = _layer.drawableSize.width, 270 | .height = _layer.drawableSize.height, 271 | .znear = 0, 272 | .zfar = 1 273 | }]; 274 | 275 | [encoder setRenderPipelineState: _renderPipelineState]; 276 | 277 | [encoder setVertexBytes: vertices 278 | length: sizeof(vertices) 279 | atIndex: 0]; 280 | 281 | [encoder setVertexBytes: texCoor 282 | length: sizeof(texCoor) 283 | atIndex: 1]; 284 | 285 | [encoder setFragmentTexture: _texutre 286 | atIndex: 0]; 287 | 288 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangleStrip 289 | indexCount: 6 290 | indexType: MTLIndexTypeUInt32 291 | indexBuffer: _indexBuffer 292 | indexBufferOffset: 0]; 293 | 294 | [encoder endEncoding]; 295 | 296 | [commandBuffer presentDrawable: currentDrawable]; 297 | 298 | [commandBuffer commit]; 299 | } 300 | 301 | @end 302 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/8. 渲染摄像头采集的 YUV 数据/RenderCameraYUV.metal: -------------------------------------------------------------------------------- 1 | //// RenderCameraYUV.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/9. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | constexpr sampler texSampler; 12 | 13 | typedef struct 14 | { 15 | float4 position [[position]]; 16 | float2 texCoor; 17 | 18 | } VertexOut; 19 | 20 | vertex VertexOut 21 | RenderCameraYUVVertexShader( 22 | uint vertexID [[ vertex_id ]], 23 | constant float4 *position [[ buffer(0) ]], 24 | constant float2 *texCoor [[ buffer(1) ]] 25 | ) { 26 | VertexOut out; 27 | 28 | out.position = position[vertexID]; 29 | out.texCoor = texCoor[vertexID]; 30 | 31 | return out; 32 | } 33 | 34 | fragment float4 35 | RenderCameraYUVFragmentShader( 36 | VertexOut in [[ stage_in ]], 37 | texture2d lumaTex [[ texture(0) ]], 38 | texture2d chromaTex [[ texture(1) ]], 39 | constant float3x3 *YUV_To_RGB_Matrix [[ buffer(0) ]], 40 | constant float3 *YUV_Translation [[ buffer(1) ]] 41 | ) { 42 | float3 yuv, rgb; 43 | 44 | yuv.r = lumaTex.sample(texSampler, in.texCoor).r; 45 | yuv.gb = chromaTex.sample(texSampler, in.texCoor).rg; 46 | 47 | rgb = YUV_To_RGB_Matrix[0] * (yuv + YUV_Translation[0]); 48 | 49 | return float4(rgb, 1); 50 | } 51 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/8. 渲染摄像头采集的 YUV 数据/RenderCameraYUVViewController.h: -------------------------------------------------------------------------------- 1 | //// RenderCameraYUVViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/9. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface RenderCameraYUVViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/8. 渲染摄像头采集的 YUV 数据/RenderCameraYUVViewController.m: -------------------------------------------------------------------------------- 1 | //// RenderCameraYUVViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/9. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "RenderCameraYUVViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | #import 13 | 14 | #import "YUV_To_RGB_Matrices_Vectors.h" 15 | 16 | #import 17 | 18 | static const float vertices[] = { 19 | -1, -1, 0, 1, // 左下角 20 | 1, -1, 0, 1, // 右下角 21 | -1, 1, 0, 1, // 左上角 22 | 1, 1, 0, 1, // 右上角 23 | }; 24 | 25 | static const float texCoor[] = { 26 | 0, 0, // 左下角 27 | 1, 0, // 右下角 28 | 0, 1, // 左上角 29 | 1, 1, // 右上角 30 | }; 31 | 32 | static const UInt32 indices[] = { 33 | 0, 1, 2, 34 | 1, 3, 2 35 | }; 36 | 37 | @interface RenderCameraYUVViewController () 38 | 39 | @end 40 | 41 | @implementation RenderCameraYUVViewController { 42 | 43 | bool _useFullRangeYUV; 44 | 45 | // AVFoundation 46 | AVCaptureSession *_session; 47 | dispatch_queue_t _captureQueue; 48 | 49 | // [Core Graphics - Metal] Texture Cache 50 | CVMetalTextureCacheRef _textureCache; 51 | 52 | // Metal 53 | id _device; 54 | 55 | id _queue; 56 | 57 | CAMetalLayer *_layer; 58 | 59 | MTLRenderPassDescriptor *_renderTargetDesc; 60 | 61 | id _renderPipelineState; 62 | 63 | id _lumaTexutre, _chromaTexture; 64 | 65 | id _indexBuffer; 66 | 67 | simd_float3x3 _YUV_To_RGB_Matrix; 68 | 69 | simd_float3 _YUV_Tranlation; 70 | } 71 | 72 | #pragma mark - Life Circle 73 | 74 | - (void)viewDidAppear:(BOOL)animated { 75 | [super viewDidAppear: animated]; 76 | 77 | _useFullRangeYUV = true; 78 | 79 | // 配置摄像头,采集 YUV 数据 80 | if (_useFullRangeYUV) { 81 | [self setupCamera: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange]; 82 | } else { 83 | [self setupCamera: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]; 84 | } 85 | 86 | [self setupMetal]; 87 | [self setupLayer]; 88 | [self setupRenderTarget]; 89 | [self setupRenderPipeline]; 90 | [self setupIndexBuffer]; 91 | [self setupTextureCache]; 92 | 93 | [AVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo 94 | completionHandler:^(BOOL granted) { 95 | if (granted) { 96 | [self->_session startRunning]; 97 | } 98 | }]; 99 | } 100 | 101 | #pragma mark - 更新 YUV 转 RGB 的 Tramsform Matrix 和 Translation Vector 102 | 103 | - (void)updateMatrixAndVector:(CVImageBufferRef)imageBuffer 104 | isFullRange:(bool)isFullRange { 105 | 106 | CFTypeRef matrixType = CVBufferGetAttachment(imageBuffer, kCVImageBufferYCbCrMatrixKey, NULL); 107 | bool use601; 108 | 109 | if (matrixType != NULL) { 110 | use601 = CFStringCompare(matrixType, kCVImageBufferYCbCrMatrix_ITU_R_601_4, 0) == kCFCompareEqualTo; 111 | } else { 112 | use601 = true; 113 | } 114 | 115 | if (use601) { 116 | _YUV_To_RGB_Matrix = isFullRange ? kColorConversion601FullRange_simd : kColorConversion601_simd; 117 | } else { 118 | _YUV_To_RGB_Matrix = kColorConversion709_simd; 119 | } 120 | 121 | _YUV_Tranlation = isFullRange ? kColorTranslationFullRange_simd : kColorTranslationVideoRange_simd; 122 | } 123 | 124 | #pragma mark - 采集的 Pixel Buffer 转换成 Metal 的 Texture 125 | 126 | - (CVMetalTextureRef)acquireTextureFromBuffer: (CVPixelBufferRef)buffer isLuma: (bool)isLuma { 127 | 128 | MTLPixelFormat format = isLuma ? MTLPixelFormatR8Unorm : MTLPixelFormatRG8Unorm; // 1 channel : 2 channel 129 | size_t planeIndex = isLuma ? 0 : 1; // 选择某一个平面 130 | 131 | CVMetalTextureRef texture; 132 | CVReturn ret = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, 133 | _textureCache, 134 | buffer, 135 | NULL, 136 | format, 137 | CVPixelBufferGetWidthOfPlane(buffer, planeIndex), // Get width of plane 138 | CVPixelBufferGetHeightOfPlane(buffer, planeIndex), // Get Height of plane 139 | planeIndex, 140 | &texture); 141 | 142 | if (ret != kCVReturnSuccess) { 143 | NSLog(@"Read texture faild from sample buffer, %d", ret); 144 | } 145 | 146 | return texture; 147 | } 148 | 149 | #pragma mark - Camera 150 | 151 | - (bool)setupCamera: (OSType)pixelFormatType { 152 | 153 | _session = [[AVCaptureSession alloc] init]; 154 | _captureQueue = dispatch_queue_create(0, 0); 155 | 156 | AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo]; 157 | device = [AVCaptureDevice defaultDeviceWithDeviceType: AVCaptureDeviceTypeBuiltInWideAngleCamera 158 | mediaType: AVMediaTypeVideo 159 | position: AVCaptureDevicePositionBack]; 160 | 161 | if (device == nil) { 162 | return false; 163 | } 164 | 165 | NSError *err; 166 | AVCaptureInput *input = [[AVCaptureDeviceInput alloc] initWithDevice: device error: &err]; 167 | 168 | if (input == nil || err != nil) { 169 | return false; 170 | } 171 | 172 | [_session beginConfiguration]; 173 | _session.sessionPreset = AVCaptureSessionPresetHigh; 174 | 175 | if (![_session canAddInput: input]) { 176 | [_session commitConfiguration]; 177 | return false; 178 | } 179 | [_session addInput: input]; 180 | 181 | AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; 182 | output.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey: @(pixelFormatType) }; 183 | [output setAlwaysDiscardsLateVideoFrames: true]; 184 | [output setSampleBufferDelegate: self queue: _captureQueue]; 185 | 186 | if (![_session canAddOutput: output]) { 187 | [_session commitConfiguration]; 188 | return false; 189 | } 190 | [_session addOutput: output]; 191 | 192 | AVCaptureConnection *connection = [output connectionWithMediaType: AVMediaTypeVideo]; 193 | 194 | if (connection == nil) { 195 | [_session commitConfiguration]; 196 | return false; 197 | } 198 | 199 | // 因为 Metal 的纹理 Y 轴和 UIKit 的是相反的,所以这里采集需要上下颠倒 200 | connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 201 | [connection setVideoMirrored: true]; 202 | 203 | [_session commitConfiguration]; 204 | return true; 205 | } 206 | 207 | - (void)captureOutput:(AVCaptureOutput *)output 208 | didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 209 | fromConnection:(AVCaptureConnection *)connection { 210 | 211 | // Metal 是线程安全的,其他线程的返回没有关系 212 | 213 | CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 214 | 215 | [self updateMatrixAndVector: imageBuffer isFullRange: _useFullRangeYUV]; // 更新 Matrix & Vector 216 | 217 | CVPixelBufferLockBaseAddress(imageBuffer, 0); 218 | 219 | CVMetalTextureRef lumaTexture = [self acquireTextureFromBuffer: imageBuffer isLuma: true]; // PixelBuffer => CV Metal Texture 220 | CVMetalTextureRef chromaTexture = [self acquireTextureFromBuffer: imageBuffer isLuma: false]; // PixelBuffer => CV Metal Texture 221 | _lumaTexutre = CVMetalTextureGetTexture(lumaTexture); // CV Metal Texture -> MTLTexture 222 | _chromaTexture = CVMetalTextureGetTexture(chromaTexture); // CV Metal Texture -> MTLTexture 223 | 224 | [self render]; // 执行渲染 225 | 226 | CVMetalTextureCacheFlush(_textureCache, 0); // 渲染完毕之后清空一下 texture cache 227 | 228 | CVPixelBufferUnlockBaseAddress(imageBuffer, 0); 229 | 230 | if (lumaTexture != NULL) { // 如果 texture 为 NULL,再 Release 就会出现 `EXC_BREAKPOINT` crash! 231 | CFRelease(lumaTexture); // 没有这个,就会不再采集!!!! 232 | } 233 | if (chromaTexture != NULL) { // 如果 texture 为 NULL,再 Release 就会出现 `EXC_BREAKPOINT` crash! 234 | CFRelease(chromaTexture); // 没有这个,就会不再采集!!!! 235 | } 236 | 237 | } 238 | 239 | #pragma mark - Metal 240 | 241 | - (void)setupMetal { 242 | 243 | _device = MTLCreateSystemDefaultDevice(); 244 | 245 | _queue = [_device newCommandQueue]; 246 | } 247 | 248 | - (void)setupLayer { 249 | 250 | _layer = [CAMetalLayer layer]; 251 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 252 | _layer.framebufferOnly = true; 253 | _layer.frame = self.view.bounds; 254 | 255 | CGFloat scale = self.view.contentScaleFactor; 256 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 257 | 258 | [self.view.layer insertSublayer: _layer atIndex: 0]; 259 | } 260 | 261 | - (void)setupRenderTarget { 262 | 263 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 264 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 265 | // colorAttachment.texture = [_layer nextDrawable].texture; 266 | colorAttachment.loadAction = MTLLoadActionClear; 267 | colorAttachment.storeAction = MTLStoreActionStore; 268 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 269 | } 270 | 271 | - (void)setupRenderPipeline { 272 | 273 | id library = [_device newDefaultLibrary]; 274 | 275 | id vertexFunc = [library newFunctionWithName: @"RenderCameraYUVVertexShader"]; 276 | id fragmentFunc = [library newFunctionWithName: @"RenderCameraYUVFragmentShader"]; 277 | 278 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 279 | pipelineDescriptor.label = @"Render Pipeline"; 280 | pipelineDescriptor.vertexFunction = vertexFunc; 281 | pipelineDescriptor.fragmentFunction = fragmentFunc; 282 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 283 | 284 | NSError *err; 285 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 286 | 287 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 288 | } 289 | 290 | - (void)setupIndexBuffer { 291 | 292 | _indexBuffer = [_device newBufferWithBytes: indices 293 | length: sizeof(indices) 294 | options: MTLResourceStorageModeShared]; 295 | } 296 | 297 | - (void)setupTextureCache { 298 | 299 | CVReturn ret = CVMetalTextureCacheCreate(NULL, NULL, _device, NULL, &_textureCache); 300 | 301 | if (ret != kCVReturnSuccess) { 302 | NSLog(@"Create cache failed"); 303 | } 304 | } 305 | 306 | - (void)render { 307 | 308 | id currentDrawable = [_layer nextDrawable]; 309 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 310 | 311 | id commandBuffer = [_queue commandBuffer]; 312 | commandBuffer.label = @"Command Buffer"; 313 | 314 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 315 | encoder.label = @"Render Command Encoder"; 316 | 317 | [encoder setViewport: (MTLViewport) { 318 | .originX = 0, 319 | .originY = 0, 320 | .width = _layer.drawableSize.width, 321 | .height = _layer.drawableSize.height, 322 | .znear = 0, 323 | .zfar = 1 324 | }]; 325 | 326 | [encoder setRenderPipelineState: _renderPipelineState]; 327 | 328 | [encoder setVertexBytes: vertices 329 | length: sizeof(vertices) 330 | atIndex: 0]; 331 | 332 | [encoder setVertexBytes: texCoor 333 | length: sizeof(texCoor) 334 | atIndex: 1]; 335 | 336 | [encoder setFragmentTexture: _lumaTexutre 337 | atIndex: 0]; 338 | 339 | [encoder setFragmentTexture: _chromaTexture 340 | atIndex: 1]; 341 | 342 | [encoder setFragmentBytes: &_YUV_To_RGB_Matrix length: sizeof(simd_float3x3) atIndex: 0]; 343 | 344 | [encoder setFragmentBytes: &_YUV_Tranlation length: sizeof(simd_float3) atIndex: 1]; 345 | 346 | [encoder drawIndexedPrimitives: MTLPrimitiveTypeTriangleStrip 347 | indexCount: 6 348 | indexType: MTLIndexTypeUInt32 349 | indexBuffer: _indexBuffer 350 | indexBufferOffset: 0]; 351 | 352 | [encoder endEncoding]; 353 | 354 | [commandBuffer presentDrawable: currentDrawable]; 355 | 356 | [commandBuffer commit]; 357 | } 358 | 359 | @end 360 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/9. 光照(冯氏光照模型)/PhongLight.metal: -------------------------------------------------------------------------------- 1 | //// PhongLight.metal 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/10. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #include 9 | using namespace metal; 10 | 11 | typedef struct 12 | { 13 | float4 position [[position]]; 14 | 15 | float3 vertexNormal; 16 | 17 | float3 fragmentPosition; 18 | 19 | } VertexOut; 20 | 21 | vertex VertexOut 22 | PhongLightVertexShader( 23 | uint vertexID [[ vertex_id ]], 24 | constant packed_float3 *position [[ buffer(0) ]], // 位置 25 | constant packed_float3 *normal [[ buffer(1) ]], // 法线 26 | constant float4x4 *modelMatrix [[ buffer(2) ]], // 本地变换矩阵 27 | constant float4x4 *viewMatrix [[ buffer(3) ]], // 观察矩阵 28 | constant float4x4 *projectionMatrix [[ buffer(4) ]] // 投影矩阵 29 | ) { 30 | VertexOut out; 31 | 32 | out.position = projectionMatrix[0] * viewMatrix[0] * modelMatrix[0] * float4(position[vertexID], 1); 33 | out.vertexNormal = (modelMatrix[0] * float4(normal[vertexID], 0)).rgb; 34 | out.fragmentPosition = (modelMatrix[0] * float4(position[vertexID], 1)).rgb; 35 | 36 | return out; 37 | } 38 | 39 | fragment float4 40 | PhongLightFragmentShader( 41 | VertexOut in [[ stage_in ]], 42 | constant float3 *originColor [[ buffer(0) ]], // 当前渲染对象的颜色 43 | constant bool *needLight [[ buffer(1) ]], // 是否需要渲染光线(光源无需反光) 44 | constant float3 *lightColor [[ buffer(2) ]], // 光源颜色 45 | constant float *ambientStrength [[ buffer(3) ]], // 环境光强度 46 | constant float3 *lightPos [[ buffer(4) ]], // 光源的位置 47 | constant float3 *eyePos [[ buffer(5) ]], // 眼睛(摄像机)的位置 48 | constant float *specularStrength [[ buffer(6) ]] // 镜面反射的强度 49 | ) { 50 | 51 | float3 color; 52 | 53 | if (needLight[0]) { 54 | 55 | // Ambient 56 | float3 ambient = ambientStrength[0] * lightColor[0]; 57 | 58 | // Diffuse 59 | float3 norm = normalize(in.vertexNormal); 60 | float3 lightDir = normalize(lightPos[0] - in.fragmentPosition); 61 | 62 | float diff = max(dot(norm, lightDir), 0.0); 63 | float3 diffuse = diff * lightColor[0]; 64 | 65 | // Specular 66 | float3 eyeDir = normalize(eyePos[0] - in.fragmentPosition); // 视线方向 67 | float3 reflectDir = reflect(-lightDir, norm); // 光线反射的方向 68 | 69 | float3 spec = pow(max(dot(eyeDir, reflectDir), 0.0), 32.0); 70 | float3 specular = specularStrength[0] * spec * lightColor[0]; 71 | 72 | color = (ambient + diffuse + specular) * originColor[0]; 73 | 74 | } else { 75 | 76 | color = originColor[0]; 77 | 78 | } 79 | return float4(color, 1); 80 | } 81 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/9. 光照(冯氏光照模型)/PhongLightViewController.h: -------------------------------------------------------------------------------- 1 | //// PhongLightViewController.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/10. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface PhongLightViewController : UIViewController 13 | 14 | @end 15 | 16 | NS_ASSUME_NONNULL_END 17 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/tests/9. 光照(冯氏光照模型)/PhongLightViewController.m: -------------------------------------------------------------------------------- 1 | //// PhongLightViewController.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/10. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "PhongLightViewController.h" 9 | 10 | #import "MetalUtils.h" 11 | 12 | #import "MetalMatrix.h" 13 | 14 | static const float vertices[] = { 15 | 16 | // 正面 17 | -1, 1, 1,// 0 18 | 1, 1, 1, // 1 19 | -1, -1, 1, // 3 20 | 21 | 1, 1, 1, // 1 22 | 1, -1, 1, // 2 23 | -1, -1, 1, // 3 24 | 25 | // 右面 26 | 1, 1, 1, // 1 27 | 1, -1, 1, // 2 28 | 1, -1, -1, // 6 29 | 30 | 1, 1, 1, // 1 31 | 1, 1, -1, // 5 32 | 1, -1, -1, // 6 33 | 34 | // 背面 35 | -1, 1, -1, // 4 36 | 1, 1, -1, // 5 37 | -1, -1, -1, // 7 38 | 39 | 1, 1, -1, // 5 40 | 1, -1, -1, // 6 41 | -1, -1, -1, // 7 42 | 43 | // 左面 44 | -1, -1, 1, // 3 45 | -1, 1, -1, // 4 46 | -1, -1, -1, // 7 47 | 48 | -1, 1, 1,// 0 49 | -1, -1, 1, // 3 50 | -1, 1, -1, // 4 51 | 52 | // 上面 53 | -1, 1, 1,// 0 54 | 1, 1, 1, // 1 55 | -1, 1, -1, // 4 56 | 57 | 1, 1, 1, // 1 58 | -1, 1, -1, // 4 59 | 1, 1, -1, // 5 60 | 61 | // 下面 62 | 1, -1, 1, // 2 63 | -1, -1, 1, // 3 64 | 1, -1, -1, // 6 65 | 66 | -1, -1, 1, // 3 67 | 1, -1, -1, // 6 68 | -1, -1, -1, // 7 69 | }; 70 | 71 | static const float normals[] = { 72 | 73 | // 正面 74 | 0, 0, 1, 75 | 0, 0, 1, 76 | 0, 0, 1, 77 | 0, 0, 1, 78 | 0, 0, 1, 79 | 0, 0, 1, 80 | 81 | // 右面 82 | 1, 0, 0, 83 | 1, 0, 0, 84 | 1, 0, 0, 85 | 1, 0, 0, 86 | 1, 0, 0, 87 | 1, 0, 0, 88 | 89 | // 背面 90 | 0, 0, -1, 91 | 0, 0, -1, 92 | 0, 0, -1, 93 | 0, 0, -1, 94 | 0, 0, -1, 95 | 0, 0, -1, 96 | 97 | // 左面 98 | -1, 0, 0, 99 | -1, 0, 0, 100 | -1, 0, 0, 101 | -1, 0, 0, 102 | -1, 0, 0, 103 | -1, 0, 0, 104 | 105 | // 上面 106 | 0, 1, 0, 107 | 0, 1, 0, 108 | 0, 1, 0, 109 | 0, 1, 0, 110 | 0, 1, 0, 111 | 0, 1, 0, 112 | 113 | // 下面 114 | 0, -1, 0, 115 | 0, -1, 0, 116 | 0, -1, 0, 117 | 0, -1, 0, 118 | 0, -1, 0, 119 | 0, -1, 0, 120 | }; 121 | 122 | @interface PhongLightViewController () 123 | 124 | @end 125 | 126 | @implementation PhongLightViewController { 127 | 128 | id _device; 129 | 130 | id _queue; 131 | 132 | CAMetalLayer *_layer; 133 | 134 | MTLRenderPassDescriptor *_renderTargetDesc; 135 | 136 | id _renderPipelineState; 137 | 138 | id _depthTexture; 139 | 140 | id _depthStencilState; 141 | 142 | id _indexBuffer; 143 | 144 | simd_float4x4 _objModelMatrix, _cameraModelMatrix, _viewMatrix, _projectionMatrix; 145 | 146 | CADisplayLink *_dis; 147 | float _cameraDistance; 148 | 149 | 150 | simd_float3 _objColor, _cameraColor; 151 | simd_float3 _lightPos, _eyePos; 152 | float _ambientStrength, _specularStrength; 153 | 154 | } 155 | 156 | #pragma mark - Life Circle 157 | 158 | - (void)viewDidLoad { 159 | [super viewDidLoad]; 160 | 161 | _cameraDistance = 10; 162 | 163 | _dis = [CADisplayLink displayLinkWithTarget: self selector: @selector(rotateCube)]; 164 | 165 | _objColor = simd_make_float3(1, 0, 0); 166 | _cameraColor = simd_make_float3(1, 1, 1); 167 | 168 | _lightPos = simd_make_float3(3, 3, 3); 169 | _eyePos = simd_make_float3(0, 0, -_cameraDistance); 170 | 171 | _ambientStrength = 0.2; 172 | _specularStrength = 0.5; 173 | } 174 | 175 | - (void)viewDidAppear:(BOOL)animated { 176 | [super viewDidAppear: animated]; 177 | 178 | [self setupMetal]; 179 | [self setupLayer]; 180 | [self setupRenderTarget]; 181 | [self setupRenderPipeline]; 182 | [self setupDepthStencilTexuture]; 183 | [self setupDepthStencil]; 184 | 185 | _objModelMatrix = [MetalMatrix mm_identity]; 186 | 187 | simd_float4x4 cameraTranslate = [MetalMatrix mm_translate: _lightPos]; 188 | simd_float4x4 cameraScale = [MetalMatrix mm_scaleWithX: 0.5 withY: 0.5 withZ: 0.5]; 189 | _cameraModelMatrix = simd_mul(cameraTranslate, cameraScale); 190 | 191 | _viewMatrix = [MetalMatrix mm_lookAtWithEyeX: _eyePos.x 192 | withEyeY: _eyePos.y 193 | withEyeZ: _eyePos.z 194 | withCenterX: 0 195 | withCenterY: 0 196 | withCenterZ: 0 197 | withUpX: 0 198 | withUpY: 1 199 | withUpZ: 0]; 200 | 201 | _projectionMatrix = [MetalMatrix mm_perspectiveWithFovy: 90 202 | withWidth: CGRectGetWidth(_layer.bounds) 203 | withHeight: CGRectGetHeight(_layer.bounds) 204 | withNear: 0.1 205 | withFar: 100]; 206 | 207 | [_dis addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSRunLoopCommonModes]; 208 | } 209 | 210 | #pragma mark - Rotate Cube 211 | 212 | - (void)rotateCube { 213 | double time = [[NSDate date] timeIntervalSince1970]; 214 | float angle = sin(time * 0.3); 215 | _objModelMatrix = [MetalMatrix mm_rotate: angle * 360 withX: 1 withY: 1 withZ: 1]; 216 | [self render]; 217 | } 218 | 219 | #pragma mark - Metal 220 | 221 | - (void)setupMetal { 222 | 223 | _device = MTLCreateSystemDefaultDevice(); 224 | 225 | _queue = [_device newCommandQueue]; 226 | } 227 | 228 | - (void)setupLayer { 229 | 230 | _layer = [CAMetalLayer layer]; 231 | _layer.pixelFormat = MTLPixelFormatBGRA8Unorm; 232 | _layer.framebufferOnly = true; 233 | _layer.frame = self.view.bounds; 234 | 235 | CGFloat scale = self.view.contentScaleFactor; 236 | _layer.drawableSize = CGSizeApplyAffineTransform(self.view.bounds.size, CGAffineTransformMakeScale(scale, scale)); 237 | 238 | [self.view.layer insertSublayer: _layer atIndex: 0]; 239 | } 240 | 241 | - (void)setupRenderTarget { 242 | 243 | _renderTargetDesc = [MTLRenderPassDescriptor renderPassDescriptor]; 244 | MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderTargetDesc.colorAttachments[0]; 245 | // colorAttachment.texture = [_layer nextDrawable].texture; 246 | colorAttachment.loadAction = MTLLoadActionClear; 247 | colorAttachment.storeAction = MTLStoreActionStore; 248 | colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 1); 249 | 250 | _renderTargetDesc.depthAttachment.clearDepth = 1; 251 | } 252 | 253 | - (void)setupRenderPipeline { 254 | 255 | id library = [_device newDefaultLibrary]; 256 | 257 | id vertexFunc = [library newFunctionWithName: @"PhongLightVertexShader"]; 258 | id fragmentFunc = [library newFunctionWithName: @"PhongLightFragmentShader"]; 259 | 260 | MTLRenderPipelineDescriptor *pipelineDescriptor = [MTLRenderPipelineDescriptor new]; 261 | pipelineDescriptor.label = @"Render Pipeline"; 262 | pipelineDescriptor.vertexFunction = vertexFunc; 263 | pipelineDescriptor.fragmentFunction = fragmentFunc; 264 | pipelineDescriptor.colorAttachments[0].pixelFormat = _layer.pixelFormat; 265 | 266 | pipelineDescriptor.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float; // depth format 267 | 268 | NSError *err; 269 | _renderPipelineState = [_device newRenderPipelineStateWithDescriptor: pipelineDescriptor error: &err]; 270 | 271 | NSAssert(_renderPipelineState != nil, @"Failed to create pipeline state: %@", err); 272 | } 273 | 274 | - (void)setupDepthStencilTexuture { 275 | 276 | _depthTexture = [MetalUtils createDepthStencilTexture: _device 277 | WithWidth: _layer.drawableSize.width 278 | withHeight: _layer.drawableSize.height]; 279 | 280 | _renderTargetDesc.depthAttachment.texture = _depthTexture; 281 | } 282 | 283 | - (void)setupDepthStencil { 284 | MTLDepthStencilDescriptor *depthStencilDesc = [MTLDepthStencilDescriptor new]; 285 | depthStencilDesc.depthCompareFunction = MTLCompareFunctionLess; 286 | depthStencilDesc.depthWriteEnabled = true; 287 | _depthStencilState = [_device newDepthStencilStateWithDescriptor: depthStencilDesc]; 288 | } 289 | 290 | - (void)render { 291 | 292 | id currentDrawable = [_layer nextDrawable]; 293 | _renderTargetDesc.colorAttachments[0].texture = currentDrawable.texture; 294 | 295 | id commandBuffer = [_queue commandBuffer]; 296 | commandBuffer.label = @"Command Buffer"; 297 | 298 | id encoder = [commandBuffer renderCommandEncoderWithDescriptor: _renderTargetDesc]; 299 | encoder.label = @"Object Render Command Encoder"; 300 | 301 | [encoder setViewport: (MTLViewport) { 302 | .originX = 0, 303 | .originY = 0, 304 | .width = _layer.drawableSize.width, 305 | .height = _layer.drawableSize.height, 306 | .znear = 0, 307 | .zfar = 1 308 | }]; 309 | 310 | [encoder setRenderPipelineState: _renderPipelineState]; 311 | 312 | // depth 313 | [encoder setDepthStencilState: _depthStencilState]; 314 | 315 | [self render_obj: encoder]; // 渲染旋转的物体 316 | 317 | [self render_camera: encoder]; // 渲染光源 318 | 319 | [encoder endEncoding]; 320 | 321 | [commandBuffer presentDrawable: currentDrawable]; 322 | 323 | [commandBuffer commit]; 324 | } 325 | 326 | - (void)render_obj: (id)encoder{ 327 | 328 | bool needLight = true; 329 | 330 | [encoder pushDebugGroup: @"Obj Draw Call"]; 331 | 332 | [encoder setVertexBytes: vertices 333 | length: sizeof(vertices) 334 | atIndex: 0]; 335 | 336 | [encoder setVertexBytes: normals 337 | length: sizeof(normals) 338 | atIndex: 1]; 339 | 340 | [encoder setVertexBytes: &_objModelMatrix 341 | length: [MetalMatrix mm_matrixSize] 342 | atIndex: 2]; 343 | 344 | [encoder setVertexBytes: &_viewMatrix 345 | length: [MetalMatrix mm_matrixSize] 346 | atIndex: 3]; 347 | 348 | [encoder setVertexBytes: &_projectionMatrix 349 | length: [MetalMatrix mm_matrixSize] 350 | atIndex: 4]; 351 | 352 | [encoder setFragmentBytes: &_objColor length: sizeof(simd_float3) atIndex: 0]; 353 | [encoder setFragmentBytes: &needLight length: sizeof(bool) atIndex: 1]; 354 | [encoder setFragmentBytes: &_cameraColor length: sizeof(simd_float3) atIndex: 2]; 355 | [encoder setFragmentBytes: &_ambientStrength length: sizeof(float) atIndex: 3]; 356 | [encoder setFragmentBytes: &_lightPos length: sizeof(simd_float3) atIndex: 4]; 357 | [encoder setFragmentBytes: &_eyePos length: sizeof(simd_float3) atIndex: 5]; 358 | [encoder setFragmentBytes: &_specularStrength length: sizeof(float) atIndex: 6]; 359 | 360 | [encoder drawPrimitives: MTLPrimitiveTypeTriangle 361 | vertexStart: 0 362 | vertexCount: 36]; 363 | 364 | [encoder popDebugGroup]; 365 | } 366 | 367 | - (void)render_camera: (id)encoder { 368 | 369 | bool needLight = false; 370 | 371 | [encoder pushDebugGroup: @"Camera Draw Call"]; 372 | 373 | [encoder setVertexBytes: vertices 374 | length: sizeof(vertices) 375 | atIndex: 0]; 376 | 377 | [encoder setVertexBytes: normals 378 | length: sizeof(normals) 379 | atIndex: 1]; 380 | 381 | [encoder setVertexBytes: &_cameraModelMatrix 382 | length: [MetalMatrix mm_matrixSize] 383 | atIndex: 2]; 384 | 385 | [encoder setVertexBytes: &_viewMatrix 386 | length: [MetalMatrix mm_matrixSize] 387 | atIndex: 3]; 388 | 389 | [encoder setVertexBytes: &_projectionMatrix 390 | length: [MetalMatrix mm_matrixSize] 391 | atIndex: 4]; 392 | 393 | [encoder setFragmentBytes: &_cameraColor length: sizeof(simd_float3) atIndex: 0]; 394 | [encoder setFragmentBytes: &needLight length: sizeof(bool) atIndex: 1]; 395 | 396 | [encoder drawPrimitives: MTLPrimitiveTypeTriangle 397 | vertexStart: 0 398 | vertexCount: 36]; 399 | 400 | [encoder popDebugGroup]; 401 | } 402 | 403 | @end 404 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/utils/MetalUtils.h: -------------------------------------------------------------------------------- 1 | //// MetalUtils.h 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/3. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import 9 | 10 | #import 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface MetalUtils : NSObject 15 | 16 | /* 17 | Texture Create & Load 18 | 19 | Note: The default texture pixel-format is RGBA8UNorm 20 | */ 21 | 22 | + (nullable id)createEmptyTexture: (id)device 23 | WithWidth: (size_t)width 24 | withHeight: (size_t)height 25 | usage: (MTLTextureUsage)usage; 26 | 27 | + (nullable id)loadImageTexture: (UIImage *)image 28 | device: (id)device 29 | usage: (MTLTextureUsage)usage; 30 | 31 | + (nullable id)loadImageTexture_CGImage: (CGImageRef)cgImage 32 | device: (id)device 33 | usage: (MTLTextureUsage)usage; 34 | 35 | + (nullable id)createDepthStencilTexture: (id)device 36 | WithWidth: (size_t)width 37 | withHeight: (size_t)height; 38 | 39 | @end 40 | 41 | NS_ASSUME_NONNULL_END 42 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/utils/MetalUtils.m: -------------------------------------------------------------------------------- 1 | //// MetalUtils.m 2 | // MetalTest 3 | // 4 | // Created by Su Jinjin on 2020/6/3. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | #import "MetalUtils.h" 9 | 10 | @implementation MetalUtils 11 | 12 | + (nullable id)createDepthStencilTexture: (id)device 13 | WithWidth: (size_t)width 14 | withHeight: (size_t)height { 15 | 16 | MTLTextureDescriptor *texDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatDepth32Float width: width height: height mipmapped: false]; 17 | texDescriptor.usage = MTLTextureUsageRenderTarget; 18 | texDescriptor.storageMode = MTLStorageModePrivate; 19 | 20 | id texture = [device newTextureWithDescriptor: texDescriptor]; 21 | 22 | return texture; 23 | } 24 | 25 | + (nullable id)createEmptyTexture: (id)device 26 | WithWidth: (size_t)width 27 | withHeight: (size_t)height 28 | usage: (MTLTextureUsage)usage { 29 | 30 | MTLTextureDescriptor *texDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatRGBA8Unorm width: width height: height mipmapped: false]; 31 | texDescriptor.usage = usage; 32 | 33 | id texture = [device newTextureWithDescriptor: texDescriptor]; 34 | 35 | return texture; 36 | } 37 | 38 | + (nullable id)loadImageTexture: (UIImage *)image 39 | device: (id)device 40 | usage: (MTLTextureUsage)usage { 41 | 42 | CGImageRef cgImage = image.CGImage; 43 | if (cgImage == nil) return nil; 44 | return [self loadImageTexture_CGImage: cgImage 45 | device: device 46 | usage: usage]; 47 | } 48 | 49 | + (nullable id)loadImageTexture_CGImage: (CGImageRef)cgImage 50 | device: (id)device 51 | usage: (MTLTextureUsage)usage { 52 | 53 | size_t width = CGImageGetWidth(cgImage); 54 | size_t height = CGImageGetHeight(cgImage); 55 | 56 | CGRect rect = CGRectMake(0, 0, width, height); 57 | 58 | size_t dataLen = width * height * 4 * sizeof(uint8_t); 59 | uint8_t *imageData = malloc(dataLen); 60 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 61 | CGContextRef ctx = CGBitmapContextCreate(imageData, 62 | width, 63 | height, 64 | 8, 65 | width * 4, 66 | colorSpace, 67 | kCGImageByteOrderDefault | 68 | kCGImageAlphaPremultipliedLast); 69 | CGContextTranslateCTM(ctx, 0, height); // 所有内容下移 height 70 | CGContextScaleCTM(ctx, 1.0f, -1.0f); // 翻转 71 | CGContextClearRect(ctx, rect); 72 | CGContextDrawImage(ctx, rect, cgImage); 73 | 74 | id texture = [MetalUtils createEmptyTexture: device 75 | WithWidth: width 76 | withHeight: height 77 | usage: usage]; 78 | 79 | if (texture == nil) return nil; 80 | 81 | [texture replaceRegion: MTLRegionMake2D(0, 0, width, height) mipmapLevel: 0 withBytes: imageData bytesPerRow: width * 4]; 82 | 83 | free(imageData); 84 | CGColorSpaceRelease(colorSpace); 85 | CGContextRelease(ctx); 86 | 87 | return texture; 88 | 89 | } 90 | 91 | @end 92 | -------------------------------------------------------------------------------- /MetalTest/MetalTest/src/utils/YUV_To_RGB_Matrices_Vectors.h: -------------------------------------------------------------------------------- 1 | //// YUV_To_RGB_Matrices_Vectors.h 2 | // OpenGLTest 3 | // 4 | // Created by Su Jinjin on 2020/5/19. 5 | // Copyright © 2020 苏金劲. All rights reserved. 6 | // 7 | 8 | /* 9 | YUV 转 RGB 矩阵 10 | 11 | Copy from GPUImage/GPUImageVideoCamera.m 12 | */ 13 | 14 | // Color Conversion Constants (YUV to RGB) including adjustment from 16-235/16-240 (video range) 15 | 16 | #import 17 | 18 | 19 | // BT.601, which is the standard for SDTV. 20 | const float kColorConversion601[] = { 21 | 1.164, 1.164, 1.164, 22 | 0.0, -0.392, 2.017, 23 | 1.596, -0.813, 0.0, 24 | }; 25 | 26 | const simd_float3x3 kColorConversion601_simd = (simd_float3x3) { 27 | (simd_float3) { 1.164, 1.164, 1.164 }, 28 | (simd_float3) { 0.0, -0.392, 2.017 }, 29 | (simd_float3) { 1.596, -0.813, 0.0 } 30 | }; 31 | 32 | // BT.709, which is the standard for HDTV. 33 | const float kColorConversion709[] = { 34 | 1.164, 1.164, 1.164, 35 | 0.0, -0.213, 2.112, 36 | 1.793, -0.533, 0.0, 37 | }; 38 | 39 | const simd_float3x3 kColorConversion709_simd = (simd_float3x3) { 40 | (simd_float3) { 1.164, 1.164, 1.164 }, 41 | (simd_float3) { 0.0, -0.213, 2.112 }, 42 | (simd_float3) { 1.793, -0.533, 0.0 } 43 | }; 44 | 45 | // BT.601 full range (ref: http://www.equasys.de/colorconversion.html) 46 | const float kColorConversion601FullRange[] = { 47 | 1.0, 1.0, 1.0, 48 | 0.0, -0.343, 1.765, 49 | 1.4, -0.711, 0.0, 50 | }; 51 | 52 | const simd_float3x3 kColorConversion601FullRange_simd = (simd_float3x3) { 53 | (simd_float3) { 1.0, 1.0, 1.0 }, 54 | (simd_float3) { 0.0, -0.343, 1.765 }, 55 | (simd_float3) { 1.4, -0.711, 0.0 } 56 | }; 57 | 58 | 59 | /* 60 | YUV 转 RGB 的变换 Translation 61 | 62 | Inspire by GPUImage 63 | */ 64 | 65 | const float kColorTranslationFullRange[] = { 66 | 0.0, -0.5, -0.5 67 | }; 68 | 69 | const simd_float3 kColorTranslationFullRange_simd = (simd_float3) { 0.0, -0.5, -0.5 }; 70 | 71 | const GLfloat kColorTranslationVideoRange[] = { 72 | -16.0 / 255.0, -0.5, -0.5 73 | }; 74 | 75 | const simd_float3 kColorTranslationVideoRange_simd = (simd_float3) { -16.0 / 255.0, -0.5, -0.5 }; 76 | --------------------------------------------------------------------------------