docs/notes/AI_CONTEXT.html
Use this document to provide expert context about the Filament rendering engine to LLMs (Gemini, Claude, ChatGPT). Copy and paste this file directly into the model's context or attach it to your project workspace.
engine->destroy(entity) to strip components, then call engine->getEntityManager().destroy(entity) to free the entity ID.delete on pointers returned by Engine. Always destroy them via engine->destroy(ptr).filament::math types (vectors, matrices, quaternions) instead of external types like GLM.VertexBuffer builders with custom material definitions (.mat).Filament is a real-time, physically-based rendering (PBR) engine. It has a strict thread separation model: a main thread where the user modifies scene state, and a render command queue thread that communicates with GPU APIs.
To render anything in Filament, you must establish these six core entities:
beginFrame, render, endFrame).Filament manages its own native allocations. To prevent segmentation faults and memory leaks, follow these rules:
EntityManager directly from the Engine instance: utils::EntityManager& em = engine->getEntityManager();Engine pointer is inaccessible: utils::EntityManager& em = utils::EntityManager::get();utils::Entity entity = engine->getEntityManager().create();engine->getEntityManager().destroy(entity);RenderableManager::Builder or LightManager::Builder on the entity:RenderableManager::Builder(1)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, vb, ib)
.material(0, matInstance)
.build(*engine, entity);
Cameras: Attach a Camera component to the entity: Camera* camera = engine->createCamera(entity);
Destroy (CRITICAL):
engine->destroy(entity); Second, free the Entity ID: engine->getEntityManager().destroy(entity);// Correct Entity Cleanup
engine->destroy(myEntity);
engine->getEntityManager().destroy(myEntity);
engine->destroyCameraComponent(entity);
- Transforms: engine->getTransformManager().destroy(entity);
- Renderables: engine->getRenderableManager().destroy(entity);Native GPU objects are created using the Engine factory or nested Builder patterns.
delete : Never call standard C++ delete on pointers returned by Engine.Engine instance.
View, Scene, Renderer, SwapChain.View* view = engine->createView();
// ...
engine->destroy(view);
Builder class, passing the engine instance to build(*engine).
VertexBuffer, IndexBuffer, Texture, Material, IndirectLight, Skybox, RenderTarget.VertexBuffer* vb = VertexBuffer::Builder()
.vertexCount(count)
.bufferCount(1)
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3)
.build(*engine);
// ...
engine->destroy(vb);
VertexBuffer::Builder setup matches the attributes declared in the custom material shader (.mat).sizeof:struct Vertex {
math::float3 position;
math::float2 uv;
};
VertexBuffer* vb = VertexBuffer::Builder()
.vertexCount(count)
.bufferCount(1)
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3, 0, sizeof(Vertex))
.attribute(VertexAttribute::TEXCOORDS, 0, VertexBuffer::AttributeType::FLOAT2, offsetof(Vertex, uv), sizeof(Vertex))
.build(*engine);
// ...
// When done, destroy the VertexBuffer:
engine->destroy(vb);
Here is the correct, boilerplate-complete structure of a Filament application:
#include <filament/Engine.h>
#include <filament/Renderer.h>
#include <filament/Scene.h>
#include <filament/View.h>
#include <filament/Camera.h>
#include <filament/SwapChain.h>
#include <utils/EntityManager.h>
using namespace filament;
void runRenderLoop(void* nativeWindow) {
// 1. Initialize Engine and Core Pipeline Objects
Engine* engine = Engine::create();
SwapChain* swapChain = engine->createSwapChain(nativeWindow);
Renderer* renderer = engine->createRenderer();
Scene* scene = engine->createScene();
View* view = engine->createView();
// 2. Set up ECS Camera
utils::Entity cameraEntity = engine->getEntityManager().create();
Camera* camera = engine->createCamera(cameraEntity);
// Configure View
view->setScene(scene);
view->setCamera(camera);
// 3. Render Loop
bool quit = false;
while (!quit) {
if (renderer->beginFrame(swapChain)) {
renderer->render(view);
renderer->endFrame();
}
}
// 4. Proper Destruction Order (Reverse of Creation)
engine->destroyCameraComponent(cameraEntity);
engine->getEntityManager().destroy(cameraEntity);
engine->destroy(view);
engine->destroy(scene);
engine->destroy(renderer);
engine->destroy(swapChain);
// Destroy Engine context last
Engine::destroy(&engine);
}
filament::math namespace.math::float2, math::float3, math::float4.math::mat3f, math::mat4f.math::quatf.