Awesome
About
Metal is an Apple framework that allows supports high performance graphics. These simple projects exploit the Lazarus Metal Package created by Ryan Joseph of the Alchemist Guild.
Installation
- You will need to have the latest version of Lazarus 2.0.6 installed on a MacOS computer (or better yet, have the latest Lazarus SVN).
- MacOS users who want to compile the Metal Projects:
- You will need to download the Alchemist Guild Lazarus Metal Package by Ryan Joseph.
- From the Lazarus graphical interface, choose Package/OpenPackageFile and select lazmetalcontrol.lpk and install this package.
- To use Metal you will need MacOS 10.12 (Sierra) or later and a Mac computer that supports Metal - typically Macs from 2012 and later.
Compiling for OpenGL
Each of these projects can be compiled to use Apple's Metal Framework. However, most of the projects can also be compiled to use either OpenGL 2.1 or OpenGL 3.3 Core. The projects that support OpenGL have a project name that ends *_gl.lpi. For example, the "lines" project includes the "lines.lpi" (Metal, MacOS only) and "lines_gl.lpi" (OpenGL). This allows you to build these projects for computers that do not support Metal (Windows, Linux and older Mac hardware).
- You should now be able to open and compile the projects provided here (the projects that end '_gl.lpi' will create OpenGL executables).
- The file "glopts.inc" allows you to specify OpenGL 2.1 or OpenGL 3.3 Core. To support the core specification, make sure
Notes
- Be aware that OpenGL clip space differs from Metal so you will need to modify your orthographic or perspective projection matrix. This explains why Ryan Joseph's VectorMath unit includes both Metal and OpenGL variations of the Perspective() and Ortho() functions (see the Mesh project for an example).
- By default Metal component will continuously refresh at your systems preferredFramesPerSecond, regardless of whether any property of your image has changed. To conserve battery life, you may want to set the' mtlControl.SetPreferredFrameRate(0);' and 'mtlControl.InvalidateOnResize := true;' and then invalidate the control only when you change the contents. This will allow on demand rendering.
- Ryan's Metal component supports multiple shaders. Multiple shaders allow nice effects like bloom and ambient occlusion. The compute and occlusion projects demonstrate this.
- By default, Metal compute shaders are executed asynchronously. So when you issue a "MTLEndCommand" command the compute shader will begin but the function returns immediately. In other words, the computation works in the background. Ryan includes the "MTLEndCommand(true)" option that will waituntilcompleted. This is a simple (though not always optimal) mechanism to ensure that one stage is completed before commencing on the next one.
- Be aware that MacOS support for Metal is still in its infancy. The integration and support for the AMD, Intel and NVidia graphics cards used in Macs may not be as strong as with Apple's own graphics cards used in its iPhones. The Mac implementation of metal does not support some features such as tile-based deferred rendering, meaning one can not always use the same code for MacOS and iOS. Until Apple releases its own graphics cards for Mac or changes its priorities, you should have realistic expectations for Metal on your Mac. Support for compute shaders has been spotty. In OpenGL/OpenCL, not-a-number values are non-signalling. However, at least on Macs Metal shaders will lock up when they encounter NaNs. You will need to design your code to avoid any edge cases that might generate NaNs (this explains the addFuzz() function in this repository's volume rendering example). There are also examples where Metal shows much poorer discard() and loop performance than more mature GLSL code running on the same hardware. Metal for Mac is clearly a work in progress, and there is a price for living on the bleeding edge. Metal will only benefit certain situations. While OpenGL may be deprecated, for many developers it may be wise to let Metal mature a bit before investing time into supporting this. Perhaps Apple will update Metal to support many handy features (like geometry shaders) or functions that will make porting OpenGL applications easier and allow Metal applications to perform faster.
Projects
With the exception of the basic project, each of these projects can compile to use the OpenGL or Metal framework. To compile for OpenGL, comment out the line '{$DEFINE METALPI}.
- A basic hello triangle example (Metal only)
- Display a 2D bitmap as a texture (Metal or OpenGL)
- A simple parallel compute project that converts a color image to grayscale (Metal only)
- Display high quality fonts (Metal or OpenGL)
- A simple OBJ/PLY mesh viewer (Metal or OpenGL)
- A variation of the mesh viewer that adds ambient occlusion (Metal only)
- Volume render NIfTI (MRI/CT) format images (Metal or OpenGL)