Class Engine
- java.lang.Object
-
- com.google.android.filament.Engine
-
public class Engine extends java.lang.ObjectEngine is filament's main entry-point.An Engine instance main function is to keep track of all resources created by the user and manage the rendering thread as well as the hardware renderer.
To use filament, an Engine instance must be created first:
import com.google.android.filament.* Engine engine = Engine.create();
Engine essentially represents (or is associated to) a hardware context (e.g. an OpenGL ES context).
Rendering typically happens in an operating system's window (which can be full screen), such window is managed by a
Renderer.A typical filament render loop looks like this:
import com.google.android.filament.* Engin engine = Engine.create(); SwapChain swapChain = engine.createSwapChain(nativeWindow); Renderer renderer = engine.createRenderer(); Scene scene = engine.createScene(); View view = engine.createView(); view.setScene(scene); do { // typically we wait for VSYNC and user input events if (renderer.beginFrame(swapChain)) { renderer.render(view); renderer.endFrame(); } } while (!quit); engine.destroyView(view); engine.destroyScene(scene); engine.destroyRenderer(renderer); engine.destroySwapChain(swapChain); engine.destroy();Resource Tracking
Each
Engineinstance keeps track of all objects created by the user, such as vertex and index buffers, lights, cameras, etc... The user is expected to free those resources, however, leaked resources are freed when the engine instance is destroyed and a warning is emitted in the console.Thread safety
An
Engineinstance is not thread-safe. The implementation makes no attempt to synchronize calls to anEngineinstance methods. If multi-threading is needed, synchronization must be external.Multi-threading
When created, the
Engineinstance starts a render thread as well as multiple worker threads, these threads have an elevated priority appropriate for rendering, based on the platform's best practices. The number of worker threads depends on the platform and is automatically chosen for best performance.On platforms with asymmetric cores (e.g. ARM's Big.Little),
Enginemakes some educated guesses as to which cores to use for the render thread and worker threads. For example, it'll try to keep an OpenGL ES thread on a Big core.Swap Chains
A swap chain represents an Operating System's native renderable surface. Typically it's a window or a view. Because a
SwapChainis initialized from a native object, it is given to filament as anObject, which must be of the proper type for each platform filament is running on.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEngine.BackendDenotes a backend
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Enginecreate()Creates an instance of Engine using the defaultEngine.Backendstatic Enginecreate(Engine.Backend backend)Creates an instance of Engine using the specifiedEngine.Backendstatic Enginecreate(java.lang.Object sharedContext)Creates an instance of Engine using theEngine.Backend.OPENGLand a shared OpenGL context.CameracreateCamera(int entity)Creates and adds aCameracomponent to a givenentity.FencecreateFence()Creates aFence.RenderercreateRenderer()Creates aRenderer.ScenecreateScene()Creates aScene.SwapChaincreateSwapChain(int width, int height, long flags)Creates a headlessSwapChainSwapChaincreateSwapChain(java.lang.Object surface)Creates an opaqueSwapChainfrom the given OS native window handle.SwapChaincreateSwapChain(java.lang.Object surface, long flags)Creates aSwapChainfrom the given OS native window handle.SwapChaincreateSwapChainFromNativeSurface(NativeSurface surface, long flags)Creates aSwapChainfrom aNativeSurface.ViewcreateView()Creates aView.voiddestroy()Destroy theEngineinstance and all associated resources.voiddestroyCameraComponent(int entity)Destroys theCameracomponent associated with the given entity.voiddestroyColorGrading(ColorGrading colorGrading)Destroys aColorGradingand frees all its associated resources.voiddestroyEntity(int entity)Destroys anentityand all its components.voiddestroyFence(Fence fence)Destroys aFenceand frees all its associated resources.voiddestroyIndexBuffer(IndexBuffer indexBuffer)Destroys aIndexBufferand frees all its associated resources.voiddestroyIndirectLight(IndirectLight ibl)Destroys aIndirectLightand frees all its associated resources.voiddestroyMaterial(Material material)Destroys aMaterialand frees all its associated resources.voiddestroyMaterialInstance(MaterialInstance materialInstance)Destroys aMaterialInstanceand frees all its associated resources.voiddestroyRenderer(Renderer renderer)Destroys aRendererand frees all its associated resources.voiddestroyRenderTarget(RenderTarget target)Destroys aRenderTargetand frees all its associated resources.voiddestroyScene(Scene scene)Destroys aSceneand frees all its associated resources.voiddestroySkinningBuffer(SkinningBuffer skinningBuffer)Destroys aSkinningBufferand frees all its associated resources.voiddestroySkybox(Skybox skybox)Destroys aSkyboxand frees all its associated resources.voiddestroyStream(Stream stream)Destroys aStreamand frees all its associated resources.voiddestroySwapChain(SwapChain swapChain)Destroys aSwapChainand frees all its associated resources.voiddestroyTexture(Texture texture)Destroys aTextureand frees all its associated resources.voiddestroyVertexBuffer(VertexBuffer vertexBuffer)Destroys aVertexBufferand frees all its associated resources.voiddestroyView(View view)Destroys aViewand frees all its associated resources.voidflushAndWait()Kicks the hardware thread (e.g.: the OpenGL, Vulkan or Metal thread) and blocks until all commands to this point are executed.Engine.BackendgetBackend()CameragetCameraComponent(int entity)Returns the Camera component of the givenentity.EntityManagergetEntityManager()LightManagergetLightManager()longgetNativeJobSystem()longgetNativeObject()RenderableManagergetRenderableManager()TransformManagergetTransformManager()booleanisValid()
-
-
-
Method Detail
-
create
@NonNull public static Engine create()
Creates an instance of Engine using the defaultEngine.BackendThis method is one of the few thread-safe methods.
- Returns:
- A newly created
Engine, ornullif the GPU driver couldn't be initialized, for instance if it doesn't support the right version of OpenGL or OpenGL ES. - Throws:
java.lang.IllegalStateException- can be thrown if there isn't enough memory to allocate the command buffer.
-
create
@NonNull public static Engine create(@NonNull Engine.Backend backend)
Creates an instance of Engine using the specifiedEngine.BackendThis method is one of the few thread-safe methods.
- Parameters:
backend- driver backend to use- Returns:
- A newly created
Engine, ornullif the GPU driver couldn't be initialized, for instance if it doesn't support the right version of OpenGL or OpenGL ES. - Throws:
java.lang.IllegalStateException- can be thrown if there isn't enough memory to allocate the command buffer.
-
create
@NonNull public static Engine create(@NonNull java.lang.Object sharedContext)
Creates an instance of Engine using theEngine.Backend.OPENGLand a shared OpenGL context.This method is one of the few thread-safe methods.
- Parameters:
sharedContext- A platform-dependant OpenGL context used as a shared context when creating filament's internal context. On Android this parameter must be an instance ofEGLContext.- Returns:
- A newly created
Engine, ornullif the GPU driver couldn't be initialized, for instance if it doesn't support the right version of OpenGL or OpenGL ES. - Throws:
java.lang.IllegalStateException- can be thrown if there isn't enough memory to allocate the command buffer.
-
isValid
public boolean isValid()
- Returns:
trueif thisEngineis initialized properly.
-
destroy
public void destroy()
Destroy theEngineinstance and all associated resources.This method is one of the few thread-safe methods.
destroy()should be called last and after all other resources have been destroyed, it ensures all filament resources are freed.Destroyperforms the following tasks:- Destroy all internal software and hardware resources.
- Free all user allocated resources that are not already destroyed and logs a warning.
This indicates a "leak" in the user's code.
- Terminate the rendering engine's thread.
Engine engine = Engine.create(); engine.destroy();
-
getBackend
@NonNull public Engine.Backend getBackend()
- Returns:
- the backend used by this
Engine
-
createSwapChain
@NonNull public SwapChain createSwapChain(@NonNull java.lang.Object surface)
Creates an opaqueSwapChainfrom the given OS native window handle.- Parameters:
surface- on Android, must be an instance ofSurface- Returns:
- a newly created
SwapChainobject - Throws:
java.lang.IllegalStateException- can be thrown if the SwapChain couldn't be created
-
createSwapChain
@NonNull public SwapChain createSwapChain(@NonNull java.lang.Object surface, long flags)
Creates aSwapChainfrom the given OS native window handle.- Parameters:
surface- on Android, must be an instance ofSurfaceflags- configuration flags, seeSwapChain- Returns:
- a newly created
SwapChainobject - Throws:
java.lang.IllegalStateException- can be thrown if the SwapChain couldn't be created- See Also:
SwapChain.CONFIG_DEFAULT,SwapChain.CONFIG_TRANSPARENT,SwapChain.CONFIG_READABLE
-
createSwapChain
@NonNull public SwapChain createSwapChain(int width, int height, long flags)
Creates a headlessSwapChain- Parameters:
width- width of the rendering bufferheight- height of the rendering bufferflags- configuration flags, seeSwapChain- Returns:
- a newly created
SwapChainobject - Throws:
java.lang.IllegalStateException- can be thrown if the SwapChain couldn't be created- See Also:
SwapChain.CONFIG_DEFAULT,SwapChain.CONFIG_TRANSPARENT,SwapChain.CONFIG_READABLE
-
createSwapChainFromNativeSurface
@NonNull public SwapChain createSwapChainFromNativeSurface(@NonNull NativeSurface surface, long flags)
Creates aSwapChainfrom aNativeSurface.- Parameters:
surface- a properly initializedNativeSurfaceflags- configuration flags, seeSwapChain- Returns:
- a newly created
SwapChainobject - Throws:
java.lang.IllegalStateException- can be thrown if theSwapChaincouldn't be created
-
destroySwapChain
public void destroySwapChain(@NonNull SwapChain swapChain)Destroys aSwapChainand frees all its associated resources.- Parameters:
swapChain- theSwapChainto destroy
-
destroyView
public void destroyView(@NonNull View view)Destroys aViewand frees all its associated resources.- Parameters:
view- theViewto destroy
-
destroyRenderer
public void destroyRenderer(@NonNull Renderer renderer)Destroys aRendererand frees all its associated resources.- Parameters:
renderer- theRendererto destroy
-
createCamera
@NonNull public Camera createCamera(int entity)
Creates and adds aCameracomponent to a givenentity.
-
getCameraComponent
@Nullable public Camera getCameraComponent(int entity)
Returns the Camera component of the givenentity.- Parameters:
entity- Anentity.- Returns:
- the Camera component for this entity or null if the entity doesn't have a Camera component
-
destroyCameraComponent
public void destroyCameraComponent(int entity)
Destroys theCameracomponent associated with the given entity.- Parameters:
entity- an entity
-
destroyScene
public void destroyScene(@NonNull Scene scene)Destroys aSceneand frees all its associated resources.- Parameters:
scene- theSceneto destroy
-
destroyStream
public void destroyStream(@NonNull Stream stream)Destroys aStreamand frees all its associated resources.- Parameters:
stream- theStreamto destroy
-
destroyFence
public void destroyFence(@NonNull Fence fence)Destroys aFenceand frees all its associated resources.- Parameters:
fence- theFenceto destroy
-
destroyIndexBuffer
public void destroyIndexBuffer(@NonNull IndexBuffer indexBuffer)Destroys aIndexBufferand frees all its associated resources.- Parameters:
indexBuffer- theIndexBufferto destroy
-
destroyVertexBuffer
public void destroyVertexBuffer(@NonNull VertexBuffer vertexBuffer)Destroys aVertexBufferand frees all its associated resources.- Parameters:
vertexBuffer- theVertexBufferto destroy
-
destroySkinningBuffer
public void destroySkinningBuffer(@NonNull SkinningBuffer skinningBuffer)Destroys aSkinningBufferand frees all its associated resources.- Parameters:
skinningBuffer- theSkinningBufferto destroy
-
destroyIndirectLight
public void destroyIndirectLight(@NonNull IndirectLight ibl)Destroys aIndirectLightand frees all its associated resources.- Parameters:
ibl- theIndirectLightto destroy
-
destroyMaterial
public void destroyMaterial(@NonNull Material material)Destroys aMaterialand frees all its associated resources.All
MaterialInstanceof the specifiedMaterialmust be destroyed before destroying it; if someMaterialInstanceremain, this method fails silently.- Parameters:
material- theMaterialto destroy
-
destroyMaterialInstance
public void destroyMaterialInstance(@NonNull MaterialInstance materialInstance)Destroys aMaterialInstanceand frees all its associated resources.- Parameters:
materialInstance- theMaterialInstanceto destroy
-
destroySkybox
public void destroySkybox(@NonNull Skybox skybox)Destroys aSkyboxand frees all its associated resources.- Parameters:
skybox- theSkyboxto destroy
-
destroyColorGrading
public void destroyColorGrading(@NonNull ColorGrading colorGrading)Destroys aColorGradingand frees all its associated resources.- Parameters:
colorGrading- theColorGradingto destroy
-
destroyTexture
public void destroyTexture(@NonNull Texture texture)Destroys aTextureand frees all its associated resources.- Parameters:
texture- theTextureto destroy
-
destroyRenderTarget
public void destroyRenderTarget(@NonNull RenderTarget target)Destroys aRenderTargetand frees all its associated resources.- Parameters:
target- theRenderTargetto destroy
-
destroyEntity
public void destroyEntity(int entity)
Destroys anentityand all its components.It is recommended to destroy components individually before destroying their
entity, this gives more control as to when the destruction really happens. Otherwise, orphaned components are garbage collected, which can happen at a later time. Even when component are garbage collected, the destruction of theirentityterminates their participation immediately.- Parameters:
entity- theentityto destroy
-
getTransformManager
@NonNull public TransformManager getTransformManager()
- Returns:
- the
TransformManagerused by thisEngine
-
getLightManager
@NonNull public LightManager getLightManager()
- Returns:
- the
LightManagerused by thisEngine
-
getRenderableManager
@NonNull public RenderableManager getRenderableManager()
- Returns:
- the
RenderableManagerused by thisEngine
-
getEntityManager
@NonNull public EntityManager getEntityManager()
- Returns:
- the
EntityManagerused by thisEngine
-
flushAndWait
public void flushAndWait()
Kicks the hardware thread (e.g.: the OpenGL, Vulkan or Metal thread) and blocks until all commands to this point are executed. Note that this does guarantee that the hardware is actually finished.This is typically used right after destroying the
SwapChain, in cases where a guarantee about the SwapChain destruction is needed in a timely fashion, such as when responding to Android'ssurfaceDestroyed.
-
getNativeObject
public long getNativeObject()
-
getNativeJobSystem
public long getNativeJobSystem()
-
-