GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
API Reference for GREM

GREM is a modular, cross-platform C++ library for building games, game engines and interactive applications.

Getting started

The source code for GREM can be found on GitHub. See the included README.md file for instructions on how to configure and build a new application project.

API organization

The API of GREM is organized into 11 main modules and one core module, with the following dependencies between them:

dot_module_hierarchy.png

Each main module is optional and can be disabled through CMake options as described in the project setup instructions, provided that they aren't required by another enabled module. Doing this can help improve compile times for applications that don't need e.g. physics or networking.

Examples

GREM provides example code in the examples/ directory, included with the repository, that shows how various features of the library work and are intended to be used. The examples are listed here, ordered by increasing complexity, along with the added functionality they demonstrate over their previously listed examples.

examples/rectangle

This example is ~80 lines long and implements a basic application that renders a rectangle.

Features

Functionality demonstrated Module used Relevant components used
Basic application structure application app::Application
Window and swapchain setup graphics gfx::Window, gfx::Device, gfx::Swapchain
Basic event handling and window resizing events evt::Eventpump
Basic 2D rendering setup graphics_2d gfx::Renderer2D, gfx::Instances2D, gfx::Camera2D, gfx::Viewport, gfx::RenderPass

examples/physics

This example is ~500 lines long and implements a basic 2D physics toy.

Features added

Functionality demonstrated Module used Relevant components used
Command line options parsing core cli
Random number generation core rng::DefaultRandomEngine, rng::UniformIntegerDistribution, rng::UniformRealDistribution
Virtual filesystem setup application app::VirtualFilesystem
Full application structure application app::Application
Frame rate-independent simulation (separating tick() from update()) application app::Application
High refresh rate support (interpolating between ticks in display() using mix()) application app::Application
Message boxes (catching and showing fatal errors to the user) events evt::SimpleMessageBox
Direct input event handling (sub-frame mouse movement and key presses) events evt::EventPump
Multithreaded executor setup execution exec::DynamicExecutor
Physics simulation setup physics phys::Simulation
Physics object/joint creation physics phys::Simulation, phys::EntityID
Image loading resource res::Image
Font loading and text rendering graphics_2d gfx::Instances2D, gfx::Font2D, gfx::Text2D
Textured rectangle rendering graphics_2d gfx::Instances2D, gfx::Texture

examples/test_game

This example game is ~700 lines long and is used to test some basic features of the library.

Features added

Functionality demonstrated Module used Relevant components used
Configuration loading from JSON files core json
Shape intersection testing (basic, without using physics) core LooseQuadtree, Box, Rectangle, Circle, Capsule
Input manager setup events evt::InputManager
User input bindings configuration events evt::InputManager
Model loading resource res::Model
Sound loading audio aud::Sound
Playing background music audio aud::SoundStage
Custom shader code (see the examples/data/test_game/shaders/ directory) graphics gfx::ShaderPipeline, gfx::FragmentShader, gfx::UniformBuffer
Offline shader compilation for Vulkan (see examples/test_game/shader_compiler.cpp for instructions) graphics gfx::FragmentShader
Sprite rendering graphics_2d gfx::Instances2D, gfx::SpriteAtlas, gfx::SpriteID
Basic 3D PBR rendering setup graphics_3d gfx::Renderer3D, gfx::Instances3D, gfx::Model3D, gfx::Fog3D, gfx::Sky3D, gfx::Decals3D, gfx::Lights3D, gfx::LightProbeVolumes3D, gfx::ReflectionProbes3D, gfx::Camera3D, gfx::Viewport, gfx::RenderPass
Dynamic lights graphics_3d gfx::Lights3D
In-game debug GUI setup imgui imgui::GraphicalUserInterface
Showing the ImGui demo window imgui imgui::GraphicalUserInterface

examples/tiles

This example is ~3500 lines, split across 19 files, and implements a tile-based 2D game.

Features added

Functionality demonstrated Relevant files Module used Relevant components used
String formatting of custom types Coordinate.hpp core Formatter
Custom fixed-point coordinates Coordinate.hpp core vec
Manual circle-vs-rectangle collision resolution Contacts.hpp core vec, Box
Custom JSON format reading Map.hpp, Schema.hpp core json
Layer stack and menu navigation Layer.hpp, layers/MenuLayer.hpp, main.cpp core ArrayList, UniquePointer, Variant
Converting mouse coordinates to world coordinates Graphics.hpp, layers/PlayLayer.hpp events evt::EventPump
Entity Component System (ECS) game architecture World.hpp execution exec::EntityRegistry, exec::ResourceRegistry, exec::Entities, exec::EntityID
Task scheduling with automatic parallelization World.hpp execution exec::EntityRegistry, exec::ResourceRegistry, exec::Entities, exec::EntityID, exec::Schedule, exec::Scheduler, exec::Executor
Advanced custom shaders WorldRenderer.hpp, shaders.hpp graphics gfx::Mesh, gfx::ShaderPipeline, gfx::VertexShader, gfx::FragmentShader, gfx::UniformBuffer

examples/platformer

This example is ~4600 lines, split across 19 files, and implements a basic third-person 3D platformer game with vehicle simulation.

Features added

Functionality demonstrated Relevant files Module used Relevant components used
3D physics simulation setup Game.hpp physics phys::Simulation
Manual simulation step scheduling with additional tasks Game.hpp physics phys::Simulation, phys::Scheduler
Raycasting, shapecasting and direct collision queries systems/player.cpp, systems/vehicle.cpp physics phys::Broadphase
Physical units and quantities in gameplay code systems/player.cpp, systems/vehicle.cpp physics phys::Quantity
3D positional audio Game.hpp, Audio.hpp, systems/player.cpp, systems/collectible.cpp audio aud::SoundStage, aud::Sound, aud::Listener
Manual instance batching and multithreaded frustum culling Graphics.hpp, systems/level.cpp graphics_3d gfx::Instances3D
Sky rendering Graphics.hpp, systems/level.cpp graphics_3d gfx::Sky3D, gfx::LightBaker3D
Custom stylized 3D model shader Graphics.hpp, shaders.hpp graphics_3d gfx::Model3D, gfx::FragmentShader, gfx::UniformBuffer, gfx::StorageBuffer
Custom JSON level format exporting and loading ../datasrc/scripts/export_level.py, systems/level.cpp core json

examples/fps

This example is ~21000 lines, split across 81 files, and implements a basic FPS game prototype with online multiplayer and splitscreen support.

This project is significantly more advanced than the other examples, and is meant to represent a more realistic integration of GREM in a production-quality engine. For example, it includes GREM headers on a per-file basis to improve compile times, and has a modular archtiecture that allows individual game systems to be compiled separately and loaded like plugins at runtime.

Features added

Functionality demonstrated Relevant files Module used Relevant components used
Dynamically loaded shared libraries GameSystems.cpp core SharedLibrary
Manual threading and synchronization Game.hpp core Thread, Mutex, ScopedLock, Atomic, AtomicFlag
Persistent user settings with associated console commands ClientSettings.hpp, GameClient.hpp, GameClient.cpp, Game.hpp core Filesystem, meta
Asset caching with parallel loading AssetCache.hpp core Filesystem, HashMap, Mutex, ScopedLock, AtomicFlag
Custom binary data serialization protocol integration serialization.hpp core Reader, SpanReader, Writer
UDP networking with a custom connection protocol Connection.hpp, GameServer.cpp, GameClient.cpp networking net::UDPSocket, net::Endpoint
Sub-frame input manager output event handling GameClient.cpp events evt::EventPump, evt::InputManager
Audio statistics visualization ClientStatisticsGraphicsStagingSystem.cpp, GameClient.cpp audio aud::SoundStage
Advanced sound instance management SoundAudioStagingSystem.cpp audio aud::SoundStage, aud::Listener
Advanced model instance management Graphics.hpp, ModelGraphicsStagingSystem.cpp graphics_3d gfx::Instances3D
Splitscreen input manager setup GameClient.cpp events evt::InputManager
Splitscreen rendering Graphics.hpp, WorldViewGraphicsRenderingSystem.cpp, GameClient.cpp graphics gfx::Texture, gfx::Viewport, gfx::RenderPass
Decal rendering Graphics.hpp, DecalGraphicsStagingSystem.cpp, GameClient.cpp graphics_3d gfx::Decals3D
Full PBR rendering with fog, light baking and global illumination Graphics.hpp, LightBakingSystem.cpp, GameClient.cpp graphics_3d gfx::Sky3D, gfx::Fog3D, gfx::LightProbeVolumes3D, gfx::ReflectionProbes3D, gfx::LightBaker3D
In-game debug GUI widgets, chat and developer console Game.hpp, GameClient.cpp, Console.hpp imgui imgui::GraphicalUserInterface

Modules

Here is a list of all modules and their most important components:

Core module

The core module is shared across all main modules and provides the following utility APIs under the grem namespace:

  • GREM/core/* - Core utilities:
    • algorithms - Range-based wrappers of standard library algorithms
    • assertions - Provides GREM_ASSERT() and grem::unreachable()
    • attributes - Portable aliases of non-standard declaration attributes that work on most compilers
    • command_line_interface - Command-line argument/option parsing
    • concepts - Collection of basic C++20 concepts
    • control - Basic control engineering utilities such as PIDController
    • Error - Base exception type for runtime errors
    • extents - Integer coordinate types for specifying regions of multidimensional data (such as images)
    • formatting - String formatting and printing utilities (replacement for std::format/std::print)
    • fundamentals - Definitions of primitive fixed-width boolean, integer and floating-point types and basic operations on them
    • geometry - Basic geometric shapes
    • math - Mathematical functions and primitives such as vector, matrix and quaternion types
    • metaprogramming - Compile-time metaprogramming utilities, including reflection of aggregate types
    • profiling - Performance profiling framework, enabled with the GREM_USE_PROFILING CMake option
    • randomness - Pseudo-random number generation (replacement for std::mt19937 and aliases of std::uniform_int_distribution, etc.)
    • statistics - Basic utilities for statistics such as ExponentialMovingAverage and SlidingWindow
    • time - Duration and time point types and utilities (aliasing std::chrono types)
    • version - Provides ABI-stable functions for querying the loaded library version at runtime
  • GREM/core/data/* - Data types:
    • Allocation - Dynamically allocated non-growable array
    • Any - Small-object-optimized type-erased wrapper for regular value types (replacement for std::any)
    • Array - Zero-allocation fixed-size array (replacement for std::array)
    • ArrayList - Dynamically allocated growable array (replacement for std::vector)
    • BitArray - Zero-allocation fixed-size packed array of bits (replacement for std::bitset)
    • BitBuffer - Dynamically allocated growable packed array of bits (replacement for std::vector<bool>)
    • Buffer - Dynamically allocated growable array that preserves erased elements for nested memory reuse (and may use malloc/realloc instead of std::allocator for trivial types)
    • Color - Floating-point linear RGBA color type
    • CStringView - Read-only view over a null-terminated string (replacement for const char*)
    • DoubleEndedQueue - Ring buffer-based double-ended queue (replacement for std::deque)
    • DoublyLinkedList - Doubly-linked list container (alias of std::list)
    • Function - Type-erased value wrapper for a callable function object (alias of std::function)
    • FunctionView - Type-erased proxy for a callable function object
    • HashMap - Linear probing hash table that maps unique keys to values (replacement for std::unordered_map)
    • HashSet - Linear probing hash table containing a set of unique keys (replacement for std::unordered_set)
    • OrderedMap - Ordered associative array mapping unique keys to values (replacement for std::map)
    • OrderedMultimap - Ordered associative array mapping keys to any number of values (replacement for std::multimap)
    • Indirect - Allocated value wrapper (replacement for std::indirect)
    • InplaceArrayList - Zero-allocation growable array with fixed capacity
    • InplaceBuffer - Zero-allocation growable array with fixed capacity that preserves erased elements for nested memory reuse
    • InplaceDoubleEndedQueue - Zero-allocation double-ended queue with fixed capacity
    • InplaceRingBuffer - Zero-allocation double-ended queue with fixed capacity that preserves erased elements for nested memory reuse
    • LinearBuffer - Append-only buffer of trivially copyable types
    • LooseOrthtree - Space subdivision structure for fast intersection tests against a large number of AABBs
    • MPMCQueue - Multi-producer multi-consumer queue
    • Optional - Optional value type (replacement for std::optional)
    • Overloaded - Utility for creating overload sets from lambdas
    • Pair - Pair of values (replacement for std::pair)
    • PriorityQueue - Priority queue container wrapper (alias of std::priority_queue)
    • RangeAllocator - Allocates contiguous integer ranges without associated memory
    • Reader - Type-erased interface for reading binary data.
    • Registry - Tightly packed set of elements with stable identifiers
    • RingBuffer - Ring buffer-based double-ended queue that preserves erased elements for nested memory reuse
    • SharedPointer - Data pointer with shared ownership (replacement for std::shared_ptr)
    • SinglyLinkedList - Singly-linked list container (alias of std::forward_list)
    • SmallArrayList - Small-buffer-optimized growable array
    • SmallBuffer - Small-buffer-optimized growable array that preserves erased elements for nested memory reuse
    • Span - View over a contiguous sequence of values (replacement for std::span)
    • SPSCQueue - Single-producer single-consumer queue
    • SPSCSlidingWindowQueue - Single-producer single-consumer queue with a sliding window of readable data
    • SquareAllocator - Allocates square integer ranges without associated memory
    • StridedSpan - View over a sequence of values with an arbitrary constant stride between them
    • String - Small-buffer-optimized owning string of characters (alias of std::string)
    • StringPool - Compact growable set of interned immutable strings with associated StringIDs
    • StringView - Read-only view over a string of characters (alias of std::string_view)
    • Table - Column-major two-dimensional structure-of-arrays container
    • Tuple - Tuple of values (replacement for std::tuple)
    • UniqueHandle - Generic resource handle with exclusive ownership
    • UniquePointer - Data pointer with exclusive ownership (replacement for std::unique_ptr)
    • Variant - Tagged union value type (replacement for std::variant)
    • Writer - Type-erased interface for writing binary data.
  • GREM/core/formats/* - Data interchange formats:
    • Adler32 - Adler-32 checksum
    • ascii - ASCII character utilities
    • base16 - Base16 (hexadecimal) string encoding/decoding
    • base64 - Base64 string encoding/decoding
    • CRC32 - Cyclic redundancy check
    • deflate - Deflate compression/decompression
    • gltf - glTF asset parsing
    • json - JSON parsing/writing/(de)serialization
    • obj - OBJ and MTL file parsing
    • unicode - UTF-8 text decoding
    • uri - Percent encoding/decoding
    • xml - Basic XML document parsing
  • GREM/core/system/* - Host system I/O:
    • Clock - Monotonic clock for measuring time intervals with high precision (alias of std::chrono::steady_clock)
    • Filesystem - Generic filesystem interface
    • InputFileHandle - Boxed generic handle to any readable file
    • OutputFileHandle - Boxed generic handle to any writable file
    • NativeFilesystem - Filesystem implementation providing direct access to the host filesystem using its native APIs
    • SharedLibrary - Library of dynamically loaded symbols (on platforms that support it)
    • Thread - Concurrent thread of execution (wrapper of std::thread) (on platforms that support multithreading)
    • synchronization - Synchronization primitives (for platforms that support multithreading):
      • Atomic - Atomic variable (wrapper of std::atomic)
      • AtomicFlag - Atomic boolean that is guaranteed to be lock-free (wrapper of std::atomic_flag)
      • AtomicRef - Temporary atomic access to a variable (wrapper of std::atomic_ref, or reimplementation on standard library versions that don't include it)
      • Mutex - Synchronization primitive for enforcing mutual exclusion (wrapper of std::mutex)
      • RecursiveMutex - Mutex that supports recursive locking (wrapper of std::recursive_mutex)
      • SharedMutex - Mutex that supports shared locking (wrapper of std::shared_mutex)
      • TimedMutex - Mutex that supports locking with a timeout (wrapper of std::timed_mutex)
      • RecursiveTimedMutex - Mutex that supports recursive locking with a timeout (wrapper of std::recursive_timed_mutex)
      • SharedTimedMutex - Mutex that supports shared locking with a timeout (wrapper of std::shared_timed_mutex)
      • ScopedLock - Lock that holds exclusive access for a mutex during a specific scope (wrapper of std::scoped_lock)
      • UniqueLock - Movable lock that holds exclusive access for a mutex (wrapper of std::unique_lock)
      • SharedLock - Movable lock that holds shared access for a shared mutex (wrapper of std::shared_lock)

Includes

GREM provides the following interface header files which transitively include all headers from their respective modules:

Alternatively, all enabled modules can be included like this:

Aliases

In addition, for applications where the potential risk of naming conflicts is acceptable, the following include can be used to provide short, global aliases for the GREM API:

This file declares using namespace grem;, promoting all names defined directly in the grem namespace to the global namespace, as well as the following shortened namespace aliases of its nested namespaces:

namespace app = grem::application;
namespace aud = grem::audio;
namespace evt = grem::events;
namespace exec = grem::execution;
namespace gfx = grem::graphics;
namespace imgui = grem::imgui;
namespace net = grem::networking;
namespace phys = grem::physics;
namespace res = grem::resource;
namespace ascii = grem::ascii;
namespace base16 = grem::base16;
namespace base64 = grem::base64;
namespace cli = grem::cli;
namespace deflate = grem::deflate;
namespace json = grem::json;
namespace meta = grem::meta;
namespace numbers = grem::numbers;
namespace obj = grem::obj;
namespace rng = grem::randomness;
namespace unicode = grem::unicode;
namespace xml = grem::xml;
Definition Application.hpp:18
Definition aliases.hpp:10
Definition Error.hpp:12
Definition aliases.hpp:11
Definition aliases.hpp:12
Definition aliases.hpp:13
Definition aliases.hpp:14
Definition controller.hpp:11
Definition Chunk.hpp:19
Definition buffer_layouts.hpp:20
Definition Error.hpp:11
Definition aliases.hpp:15
Definition aliases.hpp:16
Definition Endpoint.hpp:28
Definition aliases.hpp:17
Definition aliases.hpp:18
Definition Broadphase.hpp:24
Definition aliases.hpp:19
Definition Window.hpp:15
Definition aliases.hpp:21
Definition aliases.hpp:22

Entry point header

There is also one additional header, <GREM/entry_point.hpp>, which is not part of any particular module.

The purpose of this header is to make sure that WinMain() gets properly defined on Win32 platforms, and excluded on others. To get this portability benefit, <GREM/entry_point.hpp> should be included in your application's main source file that contains the definition of the main() function, and must not be included in any other file. It works by defining main() and/or WinMain() to call GREM_private_main(), and uses a macro to make your definition of main() actually define GREM_private_main() instead. Note that, unlike what the standard specifies for main(), this requires your definition to always take (int, char*[]) as parameters and always return an int exit code explicitly, even if they are unused.

Config header

The header <GREM/build_config.hpp> is included in all other GREM headers and contains some global macro definitions used throughout the library for e.g. DLL support on Win32 platforms. This header does not need to be included manually in user code.

Choosing a graphics backend

GREM currently provides two separate implementations of the graphics module's backend using different hardware APIs, OpenGL (3.3 Core, ES 3.0 or WebGL 2.0) and Vulkan (1.2), optimized for different use cases.

To specify which backend to use, set the GREM_GRAPHICS_BACKEND CMake option to either OpenGL or Vulkan. This defaults to OpenGL if not specified. The OpenGL backend also provides the option GREM_GRAPHICS_OPENGL_USE_ES_PROFILE, which enables the OpenGL ES profile (also used for WebGL). This defaults to OFF, but should be set to ON for web/mobile builds that don't support OpenGL Core. The included CMake preset for Emscripten already does this, but custom presets need to set it manually.

While both the OpenGL and Vulkan implementations support a common subset of features that enable the full graphics_2d and graphics_3d modules to work, they have some different pros and cons:

Advantages of the OpenGL backend

  • Required for web builds (translates to WebGL).
  • Better compatibility with mobile GPUs released around 2013-2023 (ES profile) and desktop GPUs released around 2008-2012 (Core profile).
  • Requires fewer build dependencies.
  • Natively supports runtime shader compilation (built into the user's graphics driver, doesn't require the glslang dependency).
  • Produces a slightly smaller program executable.
  • Compiles faster on the initial build of the graphics module.

Advantages of the Vulkan backend

  • Lower CPU and GPU overhead when rendering many instances (enabling higher frame rates).
  • Better compatibility with new GPUs and driver versions (potentially more future-proof).
  • Supports pre-compiled SPIR-V shader code (runtime shader compilation can be disabled entirely, shader source code can be excluded from distribution).
  • Natively supports cubemap array textures with seamless filtering (emulated with 2D array textures and sampling tricks on OpenGL, which may produce rendering artifacts in reflections, etc.).
  • Supports more compressed texture formats (limited to BC1 (DXT1) and BC3 (DXT5) on OpenGL to facilitate flipping on upload).
  • Supports grem::graphics::Device::awaitPresentation() for awaiting individual frames (when the VK_KHR_present_id and VK_KHR_present_wait extensions are available).
  • Higher depth precision (NDC space is natively 0-1 on the Z axis, OpenGL requires a conversion).
  • Better debugging tools available (validation layers, shader debugging in RenderDoc, etc.).

If you are unsure which backend best suits your specific application or target platform based on these points, the simplest option is to leave it on the OpenGL default and switch to Vulkan later if you encounter frame rate/quality issues. You can also try both to see which one yields the best performance/compatibility on different devices. In general, OpenGL works great for 2D games, while Vulkan is recommended for 3D games on platforms that support it.

Writing custom shaders

The graphics module expects any custom shaders to be written in a subset of the OpenGL Shading Language (GLSL) compatible with Vulkan 1.2, OpenGL 3.3 Core and OpenGL ES 3.0. Most of the differences between these versions are handled automatically by the graphics module, which does so by generating the necessary boilerplate as a shell around the application-provided shader code, including input/output declarations and buffer layouts specified by the shader type.

However, there are some differences between the Vulkan and OpenGL backends that are only mitigated by requiring shaders to use non-standard syntax, namely:

  • Textures must be sampled using GREM_textureSample2D(), GREM_textureSampleCubeArrayShadow(), GREM_texelFetch2D(), etc. instead of texture()/texelFetch().
  • The current vertex index must be read using GREM_vertexIndex instead of gl_VertexID.
  • The current fragment coordinates must be read using GREM_fragmentCoordinates instead of gl_FragCoord.

Another important difference is that the OpenGL ES version of the language is more strict regarding type conversions than OpenGL Core, leading to code like float x = 0; breaking portability and therefore being considered invalid (though non-ES builds may not detect this!). This example should be written float x = 0.0; in order to be portable and correct, since GLSL considers 0.0 a float while 0 is an int. Similarly, variables/parameters of type uint must use syntax like 123u instead of 123, and use explicit conversions like uint x = uint(y); when y is of type int.

Shader headers

Shaders support two kinds of #include directives: relative paths in double quotes, e.g. #include "my_shader_defines.glsl", and built-in headers provided by GREM in angle brackets, e.g. #include <GREM/tonemapping.glsl>. The provided built-in headers are:

  • <GREM/quaternion.glsl> - Provides utilities for working with quaternions.
  • <GREM/numbers.glsl> - Defines GREM_PI.
  • <GREM/tonemapping.glsl> - Provides GREM_tonemap() for tonemapping from HDR to LDR.
  • <GREM/gamma_correction.glsl> - Provides utilities for converting color to/from sRGB.
  • <GREM/blending.glsl> - Provides color compositing utilities.
  • <GREM/irradiance.glsl> - Provides functions for encoding/decoding irradiance values to/from lower-bitdepth textures.
  • <GREM/light.glsl> - Defines a PBR-compatible GREM_Light struct.
  • <GREM/pbr.glsl> - Provides utilities for physically based rendering.
  • <GREM/sampling.glsl> - Provides coordinate space translation utilities for sampling.
  • <GREM/material.glsl> - Defines a PBR-compatible GREM_Material struct.
  • <GREM/Model3D/vertex.glsl> - Provides getters for interpreting the vertex attributes of a grem::graphics::Model3D in a vertex shader.
  • <GREM/Model3D/fragment.glsl> - Provides getters for interpreting the material of a grem::graphics::Model3D in a fragment shader.
  • <GREM/Fog3D/fragment.glsl> - Provides functions for applying grem::graphics::Fog3D in a fragment shader.
  • <GREM/Sky3D/fragment.glsl> - Provides functions for sampling a grem::graphics::Sky3D in a fragment shader.
  • <GREM/Decals3D/fragment.glsl> - Provides functions for sampling a set of grem::graphics::Decals3D and applying it to a material in a fragment shader.
  • <GREM/Lights3D/fragment.glsl> - Provides functions for sampling a set of grem::graphics::Lights3D in a fragment shader.
  • <GREM/LightProbeVolumes3D/fragment.glsl> - Provides functions for sampling a set of grem::graphics::LightProbeVolumes3D in a fragment shader.
  • <GREM/ReflectionProbes3D/fragment.glsl> - Provides functions for sampling a set of grem::graphics::ReflectionProbes3D in a fragment shader.

For the full definitions of these headers, see the top of src/graphics/shaders.cpp. For examples of their usage, see the built-in shaders like shaders/graphics_3d/renderer_3d_model_3d_pbr.frag.

Custom shader headers also support the #pragma once directive as a more efficient alternative to header guards.

Shader compilation on Vulkan

Compiling custom GLSL shaders under the Vulkan backend requires the GREM_GRAPHICS_VULKAN_USE_GLSL_COMPILATION CMake option to be set to ON. This also requires Python 3 to be installed and available to CMake in order to successfully configure the SPIRV-Tools dependency.

When this option is enabled, shaders can be compiled on-the-fly on Vulkan just as they can on OpenGL, which is useful for prototyping and debugging. However, note that before distributing the application, it is recommended to pre-compile the shaders using a shader compiler program and then set this option back to OFF, as mentioned under Dependencies in README.md.

For more information on pre-compiling GLSL shaders to SPIR-V for Vulkan, see the comment in examples/test_game/shader_compiler.cpp.

API conventions

Except where the documentation specifies otherwise, the API uses the following conventions by default, in addition to those implied by the C++ standard:

Safety

  • The documentation uses the word "must" to specify that failure to meet a condition yields undefined behavior.
  • The following names identify implementation details that are not part of the public API and may be changed/removed in future versions (even patches), and must therefore not be used directly, despite potentially being accessible due to language limitations:
    • Any name in a nested detail namespace.
    • Any name starting with GREM_PRIVATE_ or GREM_private_.
    • Any member name starting with _private_.
  • Throwing functions provide the following exception safety guarantee by default: When an exception is thrown, mutable objects/arguments passed to the throwing function are left in a destructible but unspecified state with no resources leaked. In this state, the affected objects always support destruction, reassignment and noexcept functions that restore them to a known state without preconditions (like clear() and reset()), but other operations may yield undefined behavior.
  • Member functions of types in the core/data subdirectory provide whichever exception guarantee can be inferred from their current implementation. Their exception safety may be strengthened in future API versions, but they will not be weakened unless the major version is incremented.
  • Any function that is not marked noexcept may be extended to throw any exception type in a future API version of the library, without changing the ABI version.
  • Pointers, views and references returned from a member function are tied to the lifetime of the object on which the function was called.
  • Non-null pointers, views and references passed to a function, whether directly or as part of an object, must remain valid until the function returns.
  • When multiple pointers, views or references are passed to a function, and at least one of them is writable, they may not alias each other or reference overlapping memory regions.
  • Enumeration values passed to a function, whether directly or as part of an object, must be one of the valid named enumerands of the enum type, unless the enum type has no named enumerands (like byte).
  • Functions that mutate an object/argument are not thread-safe, and require exclusive access to the object until the function returns.
  • Functions declared in the graphics, graphics_2d, graphics_3d or imgui modules may only be called from the main thread of the program, i.e. the thread that invoked main(). This also includes member functions, overloaded operators, constructors and destructors from these modules, with the exception of aggregate types like grem::graphics::ModelInstance3D where all the fields are public and also do not fall under this restriction themselves.

Units and coordinate spaces

  • World coordinates and physical quantities are expressed in standard SI units (kilograms, meters, seconds, etc.).
  • Graphics and linear algebra operations use OpenGL conventions, such as normalized lower-left-origin texture coordinates, a left-handed [-1, 1] NDC space and a right-handed world coordinate system with Y up, with the following exceptions:
    • The NDC depth (Z) coordinate range is [0, 1] instead of [-1, 1] (needed to support Vulkan, affects the implementation of grem::ortho(), grem::perspective(), etc.).
    • Window-relative fragment coordinates, provided by grem::events::EventPump, grem::graphics::Window or grem::graphics::Display, or through GREM_fragmentCoordinates in shaders, are relative to an upper left origin instead of a lower left origin (also to support Vulkan).
    • Images uploaded to textures are sampled using the same texture coordinate space as textures rendered through framebuffers are, instead of appearing to be flipped vertically.
  • CPU-side image pixels are stored tightly packed in row-major order, starting at the top left, while GPU-side textures are sampled relative to the bottom left. On OpenGL, this causes a vertical flip to occur behind the scenes on image upload for consistency with framebuffer output, though not on Vulkan. The Vulkan backend stores textures in the same order as CPU images and automatically flips the sampling in the shader instead to achieve consistency.
  • Tangent-space basis matrices use TBN column order (tangent, bitangent, normal).
  • Shaders work in linear Rec. 709 color space by default and perform gamma correction to/from sRGB automatically based on texture format.
  • Shaders expect color textures with an alpha channel to be stored with pre-multiplied alpha. The output color written by shaders is also in pre-multiplied form. Bitmap CPU images are assumed have straight alpha (like PNGs) and are automatically pre-multiplied when uploaded to a texture by default.

External documentation

  • API documentation is omitted for core data structures and interfaces that mimic or closely match corresponding interfaces in the standard library or other standard APIs.
  • For C/C++ standard library documentation, the recommended resource is cppreference.com.
  • For GLSL documentation, the recommended resource is the Khronos OpenGL and OpenGL ES reference pages.

Naming conventions

GREM uses the naming conventions listed below to reduce internal naming conflicts and provide semantic hints to the reader regarding the purpose of different kinds of entities declared by the API.

There is no requirement to follow these naming conventions in user code, nor any particular recommendation to use this style, but it is provided here for reference in case anyone finds it useful for reading/extending the API or as inspiration for their own code.

Casing

  • Functions and methods use camelCase, e.g. formatString().
  • Variables, fields and parameters use camelCase, e.g. deltaTime.
  • Compile-time constants use UPPER_SNAKE_CASE, e.g. MAX_TILE_COUNT.
  • Types, structs and classes use PascalCase, e.g. AtomicFlag.
  • Non-type template parameters use PascalCase, e.g. SmallCapacity.
  • Concepts and metafunctions use snake_case, e.g. equality_comparable.
  • Macros and preprocessor definitions use UPPER_SNAKE_CASE, e.g. GREM_ALWAYS_INLINE.
  • Namespaces and filenames use snake_case, e.g. command_line_interface.hpp, unless they are associated with a specific named entity, e.g. Window.hpp.
  • Acronyms do not affect the casing of adjacent words, and are always capitalized unless they are the first word in a normally lower-camelCased name. Valid examples include:
    • SPSCQueue (type "SPSC queue")
    • fromSRGB() (function "from sRGB")
    • rgbCoefficients (parameter "RGB coefficients")

Affixes

  • Macros and preprocessor definitions are prefixed with the library name and the module they are declared in to reduce naming conflicts, e.g. GREM_PHYSICS_USE_DEBUG_VISUALIZATION.
  • Metafunction type aliases use a _t suffix, e.g. variant_alternative_t.
  • Metafunction variable aliases use a _v suffix, e.g. variant_index_v.
  • Read-only view types with reference semantics use a View suffix, e.g. ImageView.
  • Writable view types with reference semantics use a Reference suffix, e.g. ImageReference.
  • Non-abstract base classes use a Base suffix, e.g. EventBase.
  • CRTP base class templates use a Base suffix, e.g. RegistryElementIDBase.
  • Type templates with a default concrete alias use a Base suffix, e.g. StringBase.
  • Non-allocating container variants use an Inplace prefix, e.g. InplaceArrayList.
  • Small-buffer-optimized container variants use a Small prefix, e.g. SmallArrayList.

Grammar

  • Types use a noun or adjective-noun naming style (often with a noun as adjective), e.g. Window, ConvexPolytope or EntityTable.
  • Pluralized type names indicate types that represent collections of things, e.g. Contacts or Lights3D.
  • Impure procedures and non-const methods use a verb-noun or verb-adjective(s)-noun naming style by default, e.g. handleEvent() or putShadedTextInstance(). If the grammatical object is implied by a parameter or the type being acted upon, the noun part may be omitted, e.g. serialize(stream, value) or pidController.reset().
  • Pure functions and const member functions, except for common math functions like sqrt(), typically use one of the following prefixes:
    • get for getters that return a value or reference in (amortized) O(1) time, e.g. getWidth().
    • is for checks that return a boolean in (amortized) O(1) time, or O(n) time for ranges, e.g. isUppercase().
    • calculate or build for potentially expensive functions that produce a value, e.g. calculateBoundingBox() or buildSchedule().
    • find for potentially expensive functions that look something up and return it if found, or an empty/null value otherwise, e.g. findInputByIdentifier().
    • convert for functions that map a value to a logically corresponding value in a different format in O(1) time for scalars or O(n) time for ranges, e.g. convertLinearToSRGB().
    • to for potentially expensive functions that map an object to a corresponding value of a different type, e.g. toString().
    • as for functions that map an object to a reference or view of a different type in O(1) time, e.g. asBytes().
    • from for static member functions that create an instance of the associated class by converting their function arguments, e.g. Color::fromSRGB().
    • format for potentially expensive functions that build a string, e.g. formatCurrentExceptionMessage().

General

  • Pointer and reference variables are named as if they were the referenced/pointed-to value itself when the concrete pointer type is an implementation detail, e.g. elements (rather than pElements, elementPointer or pointerToElements).
  • Interfaces and abstract base classes use the same naming convention as regular types, e.g. Executor.
  • Templates use the same naming convention as the kind of entity they are a template of, e.g. ArrayList.
  • Aliases use the same naming convention as the kind of entity they are an alias of, e.g. LooseQuadtree.
  • Longer, more descriptive names are generally preferred over succinct names, e.g. inversePrincipalMomentsOfInertia over invIDiag.
  • The following short names have specific meanings in most cases:
    • x, y, z and w are coordinates related to dimensional axes.
    • r, g, b and a are red, green, blue and alpha color components.
    • a, b, c, etc. are the positional arguments of an operator or math function.
    • i and j are short-lived indices, usually loop variables.
    • it is a short-lived or often-advancing iterator, usually a loop variable or the result of find()/insert()/emplace() on a container.
    • p is a short-lived or often-advancing pointer, usually a loop variable.
    • ch is a short-lived character, usually an element of a string.
    • kv is a short-lived key-value pair, usually an element of a map.
    • output is the main out-parameter of the current function.
    • result is the return value of the current function.

Note that the naming conventions are occasionally broken in order to match a standard or external API (for familiarity, concept conformance or consistency), such as:

  • uint32_t (from the C standard library) instead of UInt32,
  • empty() (from the C++ standard library) instead of isEmpty() or
  • vec2 and sampler2D (from GLSL) instead of FloatVector2D and Sampler2D.