Recent

Author Topic: Metal Framework for MacOS  (Read 12378 times)

ChrisR

  • Full Member
  • ***
  • Posts: 236
Metal Framework for MacOS
« on: August 22, 2018, 04:09:07 pm »
I have created a few Lazarus projects that can be compiled for Apple's new Metal API.
  https://github.com/neurolabusc/Metal-Demos
Most of these projects can also be compiled to support OpenGL 3.3 Core - providing support for Windows, Linux and older Macs. By keeping a thin layer for either OpenGL or Metal, one can retain most of Lazarus' "write once, compile everywhere" magic.

  The documentation is sparse, but both the main page and the line project include some useful information
  https://github.com/neurolabusc/Metal-Demos/tree/master/lines

The Metal support uses Ryan Joseph's lazmetalcontrol
  https://github.com/genericptr/Metal-Framework
My examples all use Lazarus. However, Ryan's repository also includes some examples of how to use Metal directly from freepascal without Lazarus.

Ryan deserves incredible credit - has framework provides a great a very simple way to use this powerful but complex API.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #1 on: December 26, 2019, 12:16:46 pm »
Hi Chris,

Thanks for your demos with Metal API on macOS!

Can you check MeshProj project with Lazarus 2.0.6?

We have the following error during compilation on macOS High Sierra:

glcocoanscontext(506,45) Error: identifier idents no member "KeyEvent"
glcocoanscontext(512,45) Error: identifier idents no member "KeyEvent"
glcocoanscontext(518,45) Error: identifier idents no member "KeyEvent"

Project: Metal-Demos-master/mesh/MeshProj.lpi

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1983
  • Former Delphi 1-7, 10.2 user
Re: Metal Framework for MacOS
« Reply #2 on: December 26, 2019, 01:43:36 pm »
I've added this overlooked macOS gem to the Wiki: https://wiki.freepascal.org/Metal_Framework
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #3 on: December 26, 2019, 02:06:35 pm »
Thanks, one correction:
"to use Metal you will need macOS 10.12 (Sierra)"

Metal was introduced in macOS 10.11 (El Capitan).

ChrisR

  • Full Member
  • ***
  • Posts: 236
Re: Metal Framework for MacOS
« Reply #4 on: December 26, 2019, 02:50:59 pm »
The Lazarus 2.0.6 release for MacOS contained a regression for OpenGL support.
   https://bugs.freepascal.org/view.php?id=35797
GLCocoaNSContext.pas now requires "cocoa_extra" in uses section. You can either edit your copy or get a new copy from trunk
  https://github.com/graemeg/lazarus/tree/upstream/components/opengl

Alternatively, you can use Lazarus trunk rather than 2.0.6.   

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1983
  • Former Delphi 1-7, 10.2 user
Re: Metal Framework for MacOS
« Reply #5 on: December 26, 2019, 11:28:27 pm »
Thanks, one correction:
"to use Metal you will need macOS 10.12 (Sierra)"

Metal was introduced in macOS 10.11 (El Capitan).

Thanks, fixed!
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #6 on: December 28, 2019, 06:37:13 pm »
Chris,

Thanks! I'll try next week.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #7 on: March 05, 2020, 04:13:04 pm »
Hi,

Can you help me - rendering to texture in Metal not working.

I attach a simple test project.

It must show a rectangle, but only shows clear color (blueish). Rendering directly to screen works. If you turn off {$define Rendertarget} in MyMetal.pas, you'll see a rectangle.

ChrisR

  • Full Member
  • ***
  • Posts: 236
Re: Metal Framework for MacOS
« Reply #8 on: March 05, 2020, 06:00:16 pm »
C arrays are indexed from 0, just like dynamic Pascal arrays.

colorAttach:=RndPass.colorAttachmentAtIndex(1);   
->
colorAttach:=RndPass.colorAttachmentAtIndex(0);   

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #9 on: March 05, 2020, 06:32:23 pm »
Hi Chris,

Many thanks for your reply! I'll check tomorrow and will write about results.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #10 on: March 06, 2020, 12:19:55 pm »
Now rendering to a texture works fine. Thanks again. Stupid mistake :)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #11 on: March 11, 2020, 01:55:02 pm »
Do I understand rightly, that this Metal framework for Lazarus automatically uses Metal 2 on Mojave/Catalina?

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #12 on: June 02, 2020, 12:47:56 pm »
Hi,

Please help me. I use offscreen rendering to a texture and then save a picture into a bitmap. It works only when I resize once the main window with a mouse cursor.

I don't use MTKView and just create only MTLDevice for offscreen rendering.

A simple test project is attached. Compile and run it. rt.bmp file will be created/updated automatically. Once you resize the main window, bitmap will contain a colored gradient rectangle (OK). But why offscreen rendering is linked with a size of the main window?

ChrisR

  • Full Member
  • ***
  • Posts: 236
Re: Metal Framework for MacOS
« Reply #13 on: June 02, 2020, 03:54:12 pm »
I believe your issue is that your view is bound to the Form view, and will only be functional when the form has a context (e.g. context not ready at FormCreate, but is at FormResize):

  ViewGPU1.Parent:=Form1;
  ViewGPU1.Align:=alClient;

You might want to look at this demo:

https://developer.apple.com/documentation/metal/customizing_render_pass_setup?language=objc

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 352
Re: Metal Framework for MacOS
« Reply #14 on: June 02, 2020, 05:43:54 pm »
Hi Chris,

Thanks for your advice! I'll check that article.

 

TinyPortal © 2005-2018