Metal Acceleratedかどうかを調べる方法〜あなたのアプリのどこでMetalが暗躍しているのか #potatotips

19d7ae634445d4bd9b10c7961a462260?s=47 shu223
February 22, 2018

Metal Acceleratedかどうかを調べる方法〜あなたのアプリのどこでMetalが暗躍しているのか #potatotips

potatotips #48 (iOS/Android開発Tips共有会)での発表資料です。
https://potatotips.connpass.com/event/77702/

19d7ae634445d4bd9b10c7961a462260?s=128

shu223

February 22, 2018
Tweet

Transcript

  1. Metal Accelerated͔Ͳ͏͔Λௐ΂Δํ๏ ͋ͳͨͷΞϓϦͷͲ͜ͰMetal͕҉༂͍ͯ͠Δͷ͔ Shuichi Tsutsumi @shu223

  2. None
  3. WWDC17ͷ͜ͷਤͰɺUIKitΛؚΉ ଟ͘ͷϑϨʔϜϫʔΫ͕಺෦Ͱ MetalΛ༻͍͍ͯΔ͜ͱ͕Θ͔ͬͨ

  4. ͕ɺ

  5. ࡉ͔͍͜ͱ·Ͱ͸৭ʑͱΘ͔Βͳ͍ • UIKitͷԼճΓ͸͍͔ͭΒMetalʹͳͬͨͷ͔ʁ • UIKitͷͲͷॲཧ͕Metal AcceleratedͰɺͲͷॲཧ͕ͦ͏͡Όͳ͍ͷ͔ʁ • OpenGL΋ʢiOSͰ͸ʣࠓ͸Metalͷ্ʹ࣮૷͞Ε͍ͯΔͱAppleͷਓ͕WWDC ͷϥϘͰݴ͚ͬͯͨͲɺެࣜ৘ใ͸ͳ͍ •

    2014೥ͷWWDCͰCore Animationͷ௿ϨΠϠʹ͍ͭͯղઆ͕͋Δ͚Ͳɺ͜ ΕͬͯͲ͔͜ΒͲ͜·Ͱ͕ࠓ΋༗ޮͳ৘ใͳͷ͔ʁ • etc.
  6. Appleͷެࣜ৘ใཔΈ • API Reference • Programming Guide • Forum •

    WWDC Session • ʮWWDC LabͰதͷਓ͕ݴͬͯͨʯ ͔͠΋ɾɾɾ ͕࣌ؒܦͭͱɺެࣜ৘ใͰ΋ϝϯς͞Ε͍ͯͳ͍΋ͷ͸ূڌͱͯ͠ෆे෼ʹͳΔ
  7. ʮཪऔΓʯ͸େมˍ೉͍͠

  8. ʮMetal Accelerated͔Ͳ͏͔ʯ Λࣗ෼Ͱௐ΂ΒΕͳ͍͔ʁ

  9. Metal Accelerated͔Ͳ͏͔Λͳͥ஌Γ͍ͨͷ͔ʁ • தͷਓʹΑͬͯϝϯς͞Ε͍ͯͯɺ࠷దԽ͞Ε͍ͯΔ͜ͱ͕Θ͔Δ • ࣗ෼ͷ࠷దԽख๏͕ద੾͔Ͳ͏͔ͷ൑அࡐྉʹͳΔ • ௿ϨΠϠʹ౿ΈࠐΜͰࣗલ࣮૷͢Δ͔Ͳ͏͔ͷ൑அࡐྉʹͳΔ

  10. XcodeͷGPU Report͸ʁ

  11. MetalΛ໌ࣔతʹར༻͍ͯ͠ͳ͍ͱɺ XcodeͷGPU Report͸ग़ͯ͜ͳ͍

  12. ͦ͜ͰInstruments

  13. Metal System Trace

  14. UIKit͔͠࢖ͬͯͳ͍ʢʹMetal࢖ͬͯͳ͍ʣ ΞϓϦͰࢼͯ͠ΈΔ

  15. τϨʔεͰ͖ͯΔʂ

  16. UIKitͷԼͰʮ҉໧తʹʯ࢖ΘΕ͍ͯΔ MetalͷଉਧΛײ͡Δ͜ͱ͕Ͱ͖Δʂ

  17. ࣌ܥྻํ޲ʹ֦େͯ͠ΈΔ

  18. ͬ͘͟Γͨ͠ݟํ GPUͰ࣮ߦ͢ΔίϚϯυͷ࡞੒ʢonCPUʣ GPU΁ίϚϯυΛૹ৴ʢon CPUʣ γΣʔμͷ࣮ߦʢon GPUʣ σΟεϓϨΠ΁ͷඳը CPUͰͷॲཧ → GPUͰͷॲཧ

    → ը໘දࣔ ͷҰ࿈ͷྲྀΕͱෛՙ͕ՄࢹԽ͞Ε͍ͯΔ
  19. “Launching” Comand Buffer (SpringBoard) ͦͷޙඳըλεΫͳ͠ ʢUIKit͸ඞཁ͕ͳ͚Ε͹ߋ৽ॲཧΛ͠ͳ͍ʣ “Foreground” ͜ͷ͋ͨΓ͸ΞϓϦىಈ࣌ͷඳը ॲཧͬΆ͍

  20. ϘλϯΛԡͨ͠ΒUIImageViewͰը૾Λදࣔ͢Δ ʢ΋ͪΖΜMetal͸࢖Θͳ͍ʣ

  21. ΞϓϦىಈ࣌ͷॾʑ ϘλϯԡԼʙ UIImageViewͷը૾දࣔ

  22. ࣌ܥྻํ޲ʹ֦େ

  23. ʮMetal Accelerated͔Ͳ͏͔ʯ Λࣗ෼Ͱௐ΂ΒΕͳ͍͔ʁ → InstrumentsͰௐ΂ΒΕͦ͏

  24. ໰୊఺

  25. ϦΞϧλΠϜʹݟΕͳ͍ ʮͲͷάϥϑ͕ͲͷॲཧΛද͍ͯ͠Δͷ͔ʁʯ ͕Θ͔ΓͮΒ͍

  26. MetalͷlabelػೳΛར༻ͯ͠͸Ͳ͏͔ʁ private func pushLabel(_ label: String) { let commandBuffer =

    commandQueue.makeCommandBuffer()! commandBuffer.label = label commandBuffer.commit() } ʮԿ΋͠ͳ͍MetalͷίϚϯυόοϑΝʯΛGPUʹϓογϡ͢Δ ϝιουΛ͓͍ͭͬͯͯ͘ɺ @IBAction func btnTapped(_ sender: UIButton) { pushLabel(“start”) imageView.image = UIImage(named: "highsierra") pushLabel(“end”) } ͜Μͳײ͡ͰݺͿͱɾɾɾ label͚͍ͩ࣋ͬͯΔ
  27. Instruments্Ͱͦͷlabel͕දࣔ͞Εɾɾɾ ͳ͔ͬͨɾɾɾ

  28. ʮۭͷίϚϯυόοϑΝʯ͸ InstrumentsͰ͸ݟ͑ͳ͍ ࣮ߦ࣌ʹGPUʹૹΒΕͳ͍ͷ͔ɺ ͦ΋ͦ΋Ϗϧυஈ֊Ͱ࠷దԽ͞ΕΔͷ͔ɺetc… ʢͲ͔͜ʹ࢓༷ͱͯ͠ॻ͍ͯ͋Δ͔΋ʣ

  29. த਎ͷ͋ΔίϚϯυʹ͢Δ private func pushLabel(_ label: String) { let textureLoader =

    MTKTextureLoader(device: device) let srcTex = try! textureLoader.newTexture(name: “hoge”, scaleFactor: 2.0, bundle: nil) let descriptor = MTLTextureDescriptor.texture2DDescriptor( pixelFormat: srcTex.pixelFormat, width: srcTex.width, height: srcTex.height, mipmapped: false) let destTex = device.makeTexture(descriptor: descriptor)! let commandBuffer = commandQueue.makeCommandBuffer()! commandBuffer.label = label let blitEncoder = commandBuffer.makeBlitCommandEncoder()! blitEncoder.copy( from: srcTex, sourceSlice: 0, sourceLevel: 0, sourceOrigin: MTLOriginMake(0, 0, 0), sourceSize: MTLSizeMake(srcTex.width, srcTex.height, srcTex.depth), to: destTex, destinationSlice: 0, destinationLevel: 0, destinationOrigin: MTLOriginMake(0, 0, 0)) blitEncoder.endEncoding() commandBuffer.commit() } ຊ౰ʹཉ͍͠ͷ͸͚ͩ͜͜
  30. ੒ޭʂ ηοτͨ͠label͕ݟ͍͑ͯΔ

  31. ·ͱΊ • ͍··ͰɺMetal Accelerated͔Ͳ͏͔Λ֬ೝ͢Δʹ͸ɺެࣜ৘ใཔΈͩͬͨ - Guide, API Reference, Forum, WWDC

    Session, WWDC LabͰݴͬͯͨ, etc…ʮཪऔΓʯ ͕೉͔ͬͨ͠ - MetalΛ໌ࣔతʹ༻͍͍ͯͳ͚Ε͹ɺXcodeͷGPU Report͸൓Ԡ͠ͳ͍ • InstrumentsͷMetal System TraceΛ༻͍ΔͱɺMetalΛ໌ࣔతʹ༻͍͍ͯͳ ͍৔߹ʹ΋ɺMetalͷಈ͖Λ஌Δ͜ͱ͕Ͱ͖Δ • ϦΞϧλΠϜʹ͸τϨʔεͰ͖ͳ͍ͷͰɺ໨ҹͱͯ͠labelΛషΔํ๏΋ݕ౼ - ҰԠ͏·͍ͬͨ͘ - ͕ɺ೿खʹMetalΛ࢖͏ख๏ʹͳͬͯ͠·ͬͨͷͰɺ͓͢͢Ί͠ͳ͍