Godot Engine Analysis Notes

To facilitate technical exchange and sharing, OleLukCie Animation Studio will regularly publish technical posts on our official platform:
๐Ÿ”— https://paragraph.com/@olelukcie

I. Hierarchical Architecture (Five Layers)

1. Low-level Driver Layer (Hardware Abstraction)

  • Responsibility: Direct interaction with OS and hardware APIs to provide unified low-level interfaces.

  • Key Modules:

    • platform: Encapsulates window management, input devices, file systems, etc., for different OSes (e.g., platform\windows\display_server_windows.cpp implements Windows window creation and message loops).

    • drivers: Implements hardware drivers (Graphics: gles3, vulkan; Audio: alsa, wasapi; Physics: bullet, jolt_physics).

    • thirdparty: Wraps third-party libraries (freetype for font rendering, libpng for image decoding, etc.).

  • Characteristics: Platform-dependent code separated by OS; exposes functions to upper layers via abstract interfaces, hiding implementation details.

2. Core Service Layer (Core Services)

  • Responsibility: Provides essential engine services for resource, memory, thread management, etc.

  • Key Modules:

    • core: Core data structures and utility classes (memory management, containers, threads, math libraries, string processing, etc.).

    • scene: Implements scene tree and node system basics, defining node lifecycle and hierarchical relationships.

    • resource: Manages resource system for loading, caching, and releasing textures, models, etc.

    • servers: Manages core services (RenderingServer, PhysicsServer, AudioServer, etc.).

  • Characteristics: Game logic-independent, providing general services; accessed via singleton patterns or global interfaces (e.g., Engine::get_singleton()).

3. Feature Module Layer (Feature Modules)

  • Responsibility: Implements engine plugins for specific features, enabling modular functionality.

  • Key Modules:

    • Rendering: lightmapper_rd (lightmap baking), shader_language (shader parsing).

    • Physics & Navigation: godot_physics_2d/3d, navigation (pathfinding).

    • Audio & Multimedia: vorbis (Ogg Vorbis), minimp3 (MP3), interactive_music.

    • Network: enet (reliable UDP), websocket, webrtc (real-time communication).

    • Scripting & Toolchain: gdscript (scripting), gdnative (C++ extensions).

  • Characteristics: Modular design with low interdependencies; integrated via registration (e.g., Module::initialize()).

4. Scene and Game Logic Layer (Scene & Game Logic)

  • Responsibility: Implements game-specific content: scenes, character behaviors, interaction logic.

  • Key Modules:

    • scene: High-level nodes/components (Node2D/3D, Sprite, RigidBody, etc.).

    • animation: Animation system (skeletal, keyframe, state machines).

    • gui: UI controls (Control, Button, Label, etc.).

    • particles: Particle effects (fire, smoke, explosions).

  • Characteristics: Developer-facing API layer; used via scripts (GDScript) or visual editors; relies on lower layers without exposing implementation details.

5. Editor and Tools Layer (Editor & Tools)

  • Responsibility: Provides development environment: visual editors, debugging tools, resource importers.

  • Key Modules:

    • editor: Main editor framework (scene editor, asset browser, property inspector).

    • tools: Development utilities (script editor, debugger, profiler).

    • import: Resource import system (handles model, texture, audio formats).

  • Characteristics: Development-only; runtime-independent; extensible via plugins.

Hierarchical Interaction Example (Rendering Process)

  1. Game Logic Layer: Creates Sprite node with texture.

  2. Service Layer: RenderingServer processes draw commands.

  3. Driver Layer: Vulkan/GLES3 driver translates to GPU operations.

  4. Platform Layer: platform\windows manages window context and presents frame.

II. Module Classification

1. Core and Platform Modules

  • platform: OS-specific adaptation (Windows, Linux, etc.).

  • main: Engine entry (main.cpp), performance monitoring.

2. Text and Font Processing

  • text_server_adv: Advanced text layout (uses harfbuzz, icu4c for internationalization).

  • freetype: Font rendering via FreeType library.

3. Graphics and Rendering

  • gles3: OpenGL ES 3.0 renderer.

  • vulkan: Vulkan renderer.

  • glslang: GLSL shader compiler.

  • lightmapper_rd: Lightmap baking.

  • raycast: Embree-based raycasting (collision/occlusion).

4. Resource Formats and Import

  • gltf, fbx: 3D model import/export (fbx uses ufbx library).

  • ktx, svg, bmp, jpg, png, tga, webp, tinyexr: Image formats (svg uses ThorVG).

  • astcenc, bcdec, betsy, cvtt: Texture compression (ASTC, BC formats).

5. Physics Engines

  • godot_physics_2d/3d: Built-in physics (collision, rigid bodies).

  • jolt_physics: High-performance 3D physics (Jolt Physics).

  • navigation: Pathfinding (2D/3D navmeshes).

  • vhacd: Convex decomposition (generates collision shapes).

6. Audio and Multimedia

  • vorbis, theora, minimp3: Audio/video codecs (Ogg Vorbis, MP3).

  • interactive_music: Dynamic audio mixing.

7. Network and Communication

  • enet: Reliable UDP networking (multiplayer).

  • websocket: WebSocket protocol support.

  • webrtc: Real-time audio/video.

  • upnp: Port forwarding (via miniupnpc).

8. Scripting and Debugging

  • gdscript: GDScript parser/compiler.

  • regex: PCRE2-based regular expressions.

  • jsonrpc: JSON-RPC implementation.

9. Animation and Skeleton

  • animation: Animation blending, state machines.

  • skeleton: Skeletal animation and skinning.

  • xatlas_unwrap: UV unwrapping (texture coordinate generation).

10. Other Functional Modules

  • csg: Constructive Solid Geometry (Boolean operations).

  • gridmap: Tile-based map systems.

  • noise: Procedural noise generation (FastNoise Lite).

  • mobile_vr, webxr, openxr: VR/AR support.

  • multiplayer: Networked multiplayer systems.

  • zip: ZIP file handling.

11. Third-party Library Wrappers

  • meshoptimizer: Mesh simplification/optimization.

  • zstd, brotli, zlib: Compression algorithms.

  • mbedtls: Cryptography (TLS/SSL).

III. Design Concepts

1. Modular Architecture: Decoupling and Flexibility

  • Separation of Concerns: Functions split into independent modules (e.g., text_server_adv for text, physics for collisions).

  • Static Library Linking: Modules compiled as static libs (e.g., module_gltf.windows.editor.x86_64.lib), linked into final executable.

2. Cross-platform Adaptation

  • Platform Abstraction: OS-specific code isolated in platform directory (e.g., platform\windows vs platform\linux).

  • Unified Interfaces: Core functions (rendering, input) defined via abstract interfaces; platform implementations handle specifics (e.g., Windows uses WGL/Vulkan, others use GLX/Metal).

3. Hierarchical Design

  • Layered Structure:

    • Low-level Drivers (drivers): Hardware interaction (audio, graphics).

    • Core Services (servers): Rendering, physics, audio services.

    • Scene/Resource (scene): Game logic (nodes, animations, resources).

    • Editor (editor): Development tools (independent of runtime).

4. Third-party Integration

  • Leverages Mature Libraries:

    • Graphics: glad, vulkan, glslang, embree.

    • Text: harfbuzz, icu4c, freetype.

    • Physics: jolt_physics, vhacd.

    • Data: zlib/zstd, libpng/webp, jsoncpp.

  • **Encapsulated in **thirdparty: Compiled with engine code for standalone distribution.

5. Performance Optimization

  • Parallel Compilation: Auto-detects CPU cores (19/20 used in log), configurable via -j.

  • Resource Caching: Pre-compiles resources (e.g., godot_res.windows.editor.x86_64.obj); optimizes static libraries with Ranlib for faster linking.

IV. Compilation Process (Windows x86_64 Editor)

1. Environment Setup

  • Toolchain: Uses Visual Studio 2022 Developer Command Prompt with SCons.

  • Target: โ€œwindowsโ€ platform, โ€œx86_64โ€ architecture, โ€œeditorโ€ build.

  • Parallel Build: Defaults to 19 cores (adjustable via -j or num_jobs).

  • System Check: Detects missing Unix headers (e.g., mntent.h on Windows) without errors.

2. Core Compilation

  • Build Order:
    1. Platform Code: platform\windows for OS integration.
  1. Main Program: main directory (entry, performance monitoring).

  2. Modules & Third-party: Compiles modules (text, physics) and thirdparty (fonts, graphics).

3. Library Generation & Linking

  • Static Libraries: Object files (.obj) packaged into .lib files (e.g., module summaries, third-party deps).

  • Ranlib Optimization: Improves static library indexing for faster linking.

  • Executable Linking: Generates:

    • bin\godot.windows.editor.x86_64.exe (main editor).

    • bin\godot.windows.editor.x86_64.console.exe (console version).

4. Compilation Results

  • Duration: ~26 minutes.

  • Outputs: Editor executable and intermediate libraries.

  • Characteristics: Platform-specific code isolation, third-party integration via static linking, self-contained Windows binaries.