Numerous optimizations and dynamic batching

Posted on

Effects of dynamic batching

Thanks to many sessions dedicated to optimizing CGE rendering and animation, we have a number of new optimizations (some enabled by default, some experimental — you need to enable them yourself) and new ways to profile your applications.

  1. Various “bottlenecks” (things that noticeably affect the speed) are now drastically optimized. This includes iterating over shapes (it now uses a cache and is instant), transforming frustum (uses a different algorithm that is > 2x faster), avoiding unnecessary preparing of resources (when everything is prepared), avoiding useless passes when shadow volumes are not used.

    You’re most encouraged to test your code with the new engine version, and report the results 🙂

  2. A new optimization, enabled by default: frustum culling of the whole scene (TCastleScene.SceneFrustumCulling). This works hand-in-hand with the existing per-shape frustum culling, which can be improved when using an octree (if ssRendering is in TCastleScene.Spatial).

  3. A new experimental powerful optimization called “dynamic batching” is implemented. Multiple X3D shapes (with the same appearance) can be merged into one, and rendered using one “draw call” to OpenGL/OpenGLES. In some applications, this offers incredible speedup — if you have a lot of simple shapes with the same appearance.

    You need to activate this optimization explicitly by setting DynamicBatching (global Boolean variable) to true. See also the the relevant section in the manual.

    Please treat this optimization as “experimental” for now. There are many corner cases, and I’m not yet sure whether I covered them all. IOW, it is (temporarily) possible that this optimization will break rendering in some cases. It is also certain that we don’t use all merging possibilities, yet. This will be extended, and hardened, in the future.

    Also be aware that this optimization is not guaranteed to be beneficial. We will spend some time, each frame, analyzing which shapes are “good candidates for merging”. While I tried to make this analysis very fast, but there are definitely cases when we will waste more time than we gain. If you have thousands of shapes, all using a completely different appearance, then “dynamic batching” will not merge anything, and will only waste time trying.

  4. Another new experimental powerful optimization is implemented for animations that heavily transform the shapes (e.g. typical Spine animations). You can activate it by assigning the global variable InternalFastTransformUpdate to true.

    See the documentation of InternalFastTransformUpdate for the risks. This optimization assumes that your animations only transform shapes. If you use X3D animations to transform e.g. lights, then they will fail. I will fix it at some point, and then enable this optimization by default, always.

  5. A new simple way to observe what is rendered: just display somewhere (e.g. using TCastleLabel) SceneManager.Statistics.ToString.

  6. A new FrameProfiler, to observe what is eating time per-frame (e.g. whether the problem is in OnRender or OnUpdate).

  7. On Nintendo Switch, we are integrated with a cool profiler from Nintendo.

  8. To activate risky optimization on Aarch64, you can define CASTLE_ENGINE_ENABLE_AARCH64_OPTIMIZER symbol to true. We’re working on making them non-risky (we need to reproduce and submit some cases to FPC), at which point we will activate them automatically.

The manual page about optimizations was updated to describe various features mentioned here.

Comment on this post ➤

Configurable FPC and Lazarus location in the editor

Posted on


The CGE editor now has a “Preferences” window where you can configure the location of FPC / Lazarus.

Basically, if you use the CGE editor, you no longer have to worry about adjusting your environment variables. No need to place FPC / Lazarus in $PATH anymore, and no need to define $CASTLE_ENGINE_PATH. This should make the editor more user-friendly — I’m aware that editing environment variables isn’t comfortable for everyone on all operating systems 🙂

The editor now also displays more prominently whether it detected FPC (and what version it detected), and the “Preferences” window includes a helpful text leading to Lazarus + FPC download. All this will hopefully lead to more straightforward experience for people new to Lazarus/FPC.

(The next step will be, one day, to bundle FPC/Lazarus with CGE editor… but it’s not something I want to attempt now, for nearest release. Especially since advanced users would like to have an “unbundled” download anyway, as you probably want more control over FPC/Lazaus version.)

Comment on this post ➤

Big improvements to camera API coming soon, and check out our game on Nintendo Switch

Posted on

"Escape from the Universe" space map
  1. If you have Nintendo Switch console, by sure to check out Escape from the Universe on Nintendo Switch, a game released last month by Cat-astrophe Games.

    The game uses our Castle Game Engine for everything, of course. It depends on CGE Nintendo Switch support. A Japanese version of the game is also coming, using CGE UI localization system.

  2. I’m working on a big refactor of camera and navigation API in the engine. It is not merged yet, but you can see the in-progress work in camera-refactor branch (or see the differences from master branch).

    The current API of camera has a few drawbacks that the refactor addresses:

    • Current camera settings (“what do you see in a viewport”) are mixed with navigation logic (“how do you use keyboard/mouse to change the camera”). They are mixed within the TCamera class, and this causes various headaches when setting or auto-detecting them. All games need the same camera API, but various games need wildly different navigation logic. Many games want to just “roll their own” navigation code.

      This is addressed in the new branch, by splitting into SceneManager.Camera and SceneManager.Navigation instances. The mechanism to auto-detect their settings (based on scene bounding box, X3D nodes etc.) can be now easily disabled by SceneManager.AutoDetectCamera and SceneManager.AutoDetectNavigation .

    • Currently SceneManager.Camera may not be created before the first rendering, which is not comfortable. It causes the need for methods like SceneManager.RequiredCamera or checking SceneManager.Camera <> nil.

      In the new branch, it’s simpler. The SceneManager.Camera is never nil. In contrast, SceneManager.Navigation may be nil, and it may even remain nil forever, unless you want to use some built-in navigation logic.

    • Changing projection parameters using SceneManager.OnProjection is not comfortable. Changing projection parameters using CGE editor was not possible.

      In the new branch, you can just use SceneManager.Camera.ProjectionXxx parameters. It’s natural both in code, and in CGE editor.

    • Ideally, the class TCastle2DSceneManager should be deprecated and later removed. It should be trivially easy to get the same effect (orthographic camera, projection API comfortable for 2D games) with the base TCastleSceneManager.

    • I want to create an easy class for 3rd-person navigation. This means that input drives some character, while camera follows this character. This was requested by Robert Daniel Murphy on Patreon.

    Stay tuned for more information about this:)

Comment on this post ➤

ImageBackground node, Android SDK version bump to 28, Andrzej articles about monotonic clock on Android, FPC 3.3.1 updates

Posted on

  1. We have implemented a new X3D node ImageBackground (see the documentation). It allows to define a background using a simple image, and may be useful to view3dscene users.

    Demos are inside demo-models: background/background_image.x3dv, background/background_image_partial.x3dv, background/background_image_animated.x3d .

    It is compatible with InstantReality.

    Note that programmers using Castle Game Engine have already more powerful ways of defining a background :), e.g. you can use TCastleSceneManager.Transparent := true and then place any UI control underneath the scene manager.

  2. We have updated the Android SDK API level to 28, following Google requirements. Along with it, various services on Android versions were bumped. Also our Docker image was updated to include the proper tools already downloaded.

    Thanks go to Andrzej Kilijański for doing this!

  3. More articles about Castle Game Engine are available at Andrzej Kilijański site. In particular read about:

  4. We have updated small bits in CGE code and build tool to account for the latest Free Pascal Compiler 3.3.1 behaviour (fixed iOS and Nintendo Switch using FPC linkXXX.res files, avoid a flood of warnings caused by new FPC “case” completeness analysis). Also FPC version inside our Docker image was updated.

Comment on this post ➤

Castle Game Engine on Raspberry Pi

Posted on

New Raspberry Pi in Michalis hands :)
The Unholy Society ( ) on Raspberry Pi
The Unholy Society ( ) on Raspberry Pi

Recently I got my Raspberry Pi 4 device, and I am excited by the possibilities (small+cheap device with fully functional Linux). I was happy to see that Castle Game Engine runs perfectly on it 🙂 Here are some screenshots of The Unholy Society running on Raspberry Pi.

If you’re interested in using CGE on Raspberry Pi:

  • Just install FPC using the package repository (apt-get install fpc)
  • Get CGE source code

  • Build CGE build tool running castle_game_engine/tools/build-tool/

  • Build your application as usual, running castle-engine compile.

  • Raspberry Pi is just Linux OS + ARM CPU, rendering using OpenGLES. Everything just works, no surprises 🙂

  • It should also be possible to run Lazarus and CGE editor, although I didn’t do it yet.

  • Note that the default backend uses X. It is also possible to get CastleWindow backend using DispmanX (without X), although it is not yet merged into latest CGE.

Have fun with CGE on all your devices!

Comment on this post ➤

Streaming sound tracks, FMOD linking improved

Posted on

Castle Game Engine logo with title
  1. Our sound engine now supports streaming sounds.

    “Streaming” means that we decompress the sound (like OggVorbis) during playback, in parts (as needed). This technique allows much faster sound loading (loading sound as “streamed” is almost instant). It adds a small run-time overhead to playing given sound, although in practice it is not noticeable at all, even on mobile devices (where the benefits — much faster loading — will be very noticeable 🙂 ).

    In general case, I advise to use it for longer sounds (like music tracks).

    The default remains to load sounds “complete”, just like before.

    To use this feature, call LoadBuffer method with slStreaming or use <sound ... stream="true" /> in sounds XML file. See the manual about sound for general information how to play sounds, including LoadBuffer and sounds XML files. An example is in our play_sounds example, just uncomment proper lines in gamestatemain.pas and CastleEngineManifest.xml (search for “streaming”).

    This feature works with both OpenAL and FMOD sound backends.

    Thousand thanks for implementing this feature go to Andrzej Kilijański!

  2. FMOD dynamic loading is now improved, which is particularly useful for Linux users.

    • The FMOD library no longed needs to be present at compile-time on Linux, it only needs to be present at run-time. You no longer need to pass -k-L... to FPC to link with FMOD.

    • If the FMOD library is not present at run-time, we make a warning (in log) and continue using existing backend. In effect, you can have the application use FMOD if possible, but fallback on OpenAL. This applies to all platforms using dynamic loading of FMOD, like Windows and Linux.

    • It fixes problems with linking with FMOD using GNU binutils 2.32 (like in Debian
      Testing). The linker was previously reporting errors bad value.

Comment on this post ➤

ASTC texture compression, progress of engine Delphi compatibility

Posted on

Car exported from Blender 2.8 as Wavefront OBJ
  1. Castle Game Engine now supports ASTC, which is a great GPU compresion method available on most modern mobile devices (Android and iOS). You can easily compress your textures, and then use them at runtime, using the appropriate declaration in material_properties.xml file.

    The usage is described in details in ASTC compression support added to Castle Game Engine (also available in Polish) article by Andrzej Kilijański, who implemented this feature. Thousand thanks!

  2. I pushed Delphi compatibility further. Many more units are supported, in particular our file reading/writing/searching system using URLs, including our XML support (used throughout the engine). To have uniform XML support, I introduced Delphi units DOM, XMLRead, XMLWrite that wrap Delphi XML implementation (using MSXML and COM interfaces) in an API compatible with analogous FPC units.

    It all can be tested by compiling Delphi example in CGE sources. I hope to make more progress very soon 🙂

P.S. More cool stuff from Andrzej is coming — streaming music 🙂

P.P.S. Blender 2.8 was released! Note that CGE supports various 3D model formats supported by Blender 2.8 out-of-the-box (Wavefront OBJ, glTF 2.0, X3D, Collada, STL). (Unfortunately, exporting to the best formats — glTF and X3D — is far from perfect now in Blender 2.8. E.g. glTF exporter doesn’t apply modifiers, materials remain gray, transformation hierarchy has duplicated nodes. And X3D exporter can crash. As a workaround, OBJ exporter always works 🙂 )

Comment on this post ➤

Improvements to AdMob service, list of engine features added since last release, and more articles about CGE from Andrzej Kilijański

Posted on

Castle Game Engine logo with title

Andrzej Kilijański wrote a few very nice articles about Castle Game Engine recently:

  1. Updates in Castle Game Engine Admob Service (also available in Polish) describing his recent improvements to our AdMob Android Service.

    We support now reward ads from AdMob, and the callback TAds.OnFullScreenAdClosed provides now detailed information why the user did not watch ad.

    See also wiki page listing breaking changes in CGE for more information how you can upgrade your code.

  2. Castle Game Engine stable 6.4 or beta 6.5 which version to use? (also available in Polish).

    Spoiler: use 6.5 🙂 It has been a while since the last stable version, and while we’re working on the next stable release (6.6), right now it makes more sense to use the “snapshot” 6.5 version (the download link is at the main page), especially for new projects, than the older stable 6.4.

    See also (much longer 🙂 ) complete list of changes since the last stable release.

  3. Free Pascal OpenGL App – Exception on window close (also available in Polish). This may affect you if you use Linux with proprietary NVidia drivers and stable FPC. The solution is to upgrade your FPC version, a process described in detail in the article.

Thousand thanks go to Andrzej Kilijański for writing these articles, and for continuously submitting new improvements to CGE! You can follow all his posts here, or you can also follow only CGE-related stuff in English or CGE-releated stuff in Polish.

Andrzej also added ASTC (texture compression) support to CGE lately, which is a nice GPU-compressed format available on most mobile devices (starting from 2nd generation of Mali-T600 series, Adreno 400, Apple A8). CGE now understands it (when ASTC images are inside KTX file format) and can auto-generate textures compressed to ASTC.

Comment on this post ➤

Localization improvements (and a Japanese version of “Escape from the Universe”)

Posted on


I implemented a new comfortable workflow for localization using the CastleLocalizationGetText unit. It is documented in details in the manual about localization and is demonstrated by a revised examples/localization/gettext/ demo.

It was tested by localizing to Japanese an upcoming release of “Escape from the Universe” on Nintendo Switch. I will make more news about the game itself later 🙂

Details of new localization features:

  • Supports localizing user interface (designed using CGE Editor) really easily (just call TranslateAllDesigns). In more complicated cases, you have TranslateDesign or TranslateProperties.
  • It can generate GetText PO template files, to serve as immediate starting point for translators (use GenerateGetTextPo).

  • You can use CastleTranslateResourceStrings to translate strings declared in Pascal code as resourcestring. This is just a simple wrapper for FPC GetText.TranslateResourceStrings (it understands CGE URLs, works on all CGE platforms including Nintendo Switch etc.).

  • It’s extensible by overriding TCastleComponent.TranslateProperties. A single component can have multiple properties to translate (e.g. TCastleEdit has Text, Placeholder).

  • texture-font-to-pascal tool gets additional command-line option --sample-get-text-mo to add all characters mentioned in given GetText MO file.

Everything is documented, read and follow the links from our manual about localization.

P.S. In related news, the “Escape from the Universe” English version (for Europe and America) just passed the review by Nintendo, and it will be available on Nintendo Switch since August. I’ll post more information about it later 🙂 What it means for you is that Nintendo is completely fine with games being developed with Castle Game Engine 🙂

Comment on this post ➤

Many Animations Improvements: glTF, simultaneous anims, view3dscene panel, optimizations

Posted on

Exorcist from "The Unholy Society" (
Demo of simultaneous animations from glTF
  1. Animating transformations (translation, rotation, scale) from the glTF format is supported now.

  2. Playing, cross-fading and resetting the animations is now optimized, and exposes some new features.

    New methods available now:

    We now store and use more efficiently the “reset” state of the animation. See the new TimeSensor.detectAffectedFields . Previously, our Spine importer had special (and unoptimal) code to reset the state of bones not affected by the current animation. This mechanism is now more uniform (used by all animations, e.g. those imported from glTF too) and faster (we internally call ResetAnimationState just once for an animation start).

    Using the TTimeSensorNode.Start and TTimeSensorNode.Stop you can control simultaneous animations within one scene. See the demo examples/animations/simultaneous_animations_one_scene, it controls two independent animations defined in a single glTF file.

  3. view3dscene has a new “Animations” panel to test animations comfortably.

    • Show it using the toolbar button “Animations”, or menu item “Animations -> Animations Panel”, or by pressing Ctrl+A.

    • It allows to run animations easily, controlling their looping, forward, transition (cross-fade time), speed.

    • It adds buttons to “Stop Animation” and “Reset Animation State”, performing appropriate scene methods.

  4. Fun fact: this commit was one of the most difficult commits to write during this work.

    And the commit only adds a (60-line) comment in the middle of the code. The comment contains a proof that actually no code is necessary to achieve what I need! 🙂

    There was actually another commit to demo-models to interactively test that my proof is right. The scene is in demo-models animation/spine_animation_blending_test subdirectory, you can open the exported/test_animation_blending.json file with view3dscene.

    And, of course, the next day I refactored the code, and made the comment shorter too 🙂

Comment on this post ➤