Offscreen Rendering
Topics in this article:
- A Docker (Debian) based offscreen rendering example
- Offscreen rendering by using various drivers
- Accessing backbuffer types for integrations
General Scene Rasterization Example (Debian Docker)
Prerequisite: Docker Desktop, .NET SDK 8.0 (arbitrary)
Start with a Linux based, Microsoft preconfigured .NET SDK:
Paste the following code content for 'Program.cs' into the nano editor, replacing any existing code. Then save and close the editor ([CTRL] + X, Y, Enter).
This example code makes use of the (new in 7.2) direct scene rasterization via 'scene.SaveBitmap()'.
Now compile and run our simple rendering app:
The ilnplot directory now contains a newly created 'image.bmp' file with content similar to this plot. If your content differs significantly, make sure to have activated this developer (container) seat before running 'dotnet run' again.
Driver based Offscreen Rendering
Some drivers are prepared for off-screen rendering. They require no visual surface. In order to save a scene as PNG file, one needs to provide the final resolution and the scene to be rendered:
We simply made a new scene from scratch and use it with an GDIDriver for saving. Just as well, one may take an existing scene from any other driver or Panel.Scene and use it with GDIDriver. Note that no precautions are necessary regarding shared scenes. Scenes can be reused at any time and rendered concurrently in multithreading setups.
The same principle applies to other drivers. In order to save a scene (for example from an existing panel ilPanel1.Scene) as static SVG vector graphics file ‘test.svg’:
Note that ILNumerics SVG driver is capable of handling arbitrary 2D and 3D scenes. However, because most (if not all) current SVG rendering engines nowadays render SVG primitives in antialiasing mode, rendering artifacts will be visible for abutting edges. We do our best to minimize their disturbing effect. Nevertheless, one should keep this in mind if attempting to render complex 3D scenes in SVG. The effect is 'by design'.
A nice example for offscreen rendering with ILNumerics has been given by Scott Hanselmann on his blog.
Integrating ILNumerics into other Frameworks
The ILNumerics Visualization Engine can be integrated to work seamlessly with other UI frameworks. It supports the following ways to create visual output:
- Pixel based rendering (rasterization):
- Software rendering: GDIDriver, all platforms
- Hardware pipeline supported rendering: OpenGLDriver, WindowsForms / WPF (version 7.2.: on Windows).
- SVG rendering: SVGDriver
- XML rendering: XMLDriver (as intermediate representation for later rasterization stages)
For interactivity and full platform compatibility GDIDriver is currently (version 7.2.) recommended. It automatically selects GDI+ on Windows and Skia on other platforms for rasterizing arbitrary scenes, is well optimized for good qualitiy and reasonable rendering speed.
GDI+ based Rendering (Windows)
When native GDI+ libraries are available at runtime GDIDriver.BackBuffer is of type BMPBackBuffer and uses a System.Drawing.Bitmap for rasterizing scenes. The bitmap can be accessed for reading:
Array based Rendering (all Platforms)
For non-Windows platforms or if GDIDriver.IsGDIPlusSupported is set to false a platform neutral backbuffer based on Array<int> is used as GDIDriver.BackBuffer. Rasterizing is performed solely on this array in memory, partly using Skia for text rasterization. The backbuffer is accessible in two variants:
This produces the following result (in a Console application) :
The array buffer returned references the same memory as is used for rendering. No copy is performed. A new buffer is created for the same instance of GDIDriver only, when the size of the buffer changes.
One could also save the memory as image:
... resulting in the expected output: