-
public class RendererA
Rendererinstance represents an operating system's window.Typically, applications create a
Creation and DestructionRendererper window. TheRenderergenerates drawing commands for the render thread and manages frame latency. A Renderer generates drawing commands from a View, itself containing a Scene description.A
Rendereris created using createRenderer and destroyed using destroyRenderer.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classRenderer.DisplayInfoInformation about the display this renderer is associated to
public classRenderer.FrameRateOptionsUse FrameRateOptions to set the desired frame rate and control how quickly the systemreacts to GPU load changes.interval: desired frame interval in multiple of the refresh period, set in DisplayInfo(as 1 / DisplayInfo.refreshRate)The parameters below are relevant when some Views are using dynamic resolution scaling:headRoomRatio: additional headroom for the GPU as a ratio of the targetFrameTime.Useful for taking into account constant costs like post-processing orGPU drivers on different platforms.history: History size. higher values, tend to filter more (clamped to 30)scaleRate: rate at which the gpu load is adjusted to reach the target frame rateThis value can be computed as 1 / N, where N is the number of framesneeded to reach 64% of the target scale factor.Higher values make the dynamic resolution react faster.
public classRenderer.ClearOptionsClearOptions are used at the beginning of a frame to clear or retain the SwapChain content.
-
Field Summary
Fields Modifier and Type Field Description public final static intMIRROR_FRAME_FLAG_COMMITpublic final static intMIRROR_FRAME_FLAG_SET_PRESENTATION_TIMEpublic final static intMIRROR_FRAME_FLAG_CLEAR
-
Method Summary
Modifier and Type Method Description voidsetDisplayInfo(@NonNull() Renderer.DisplayInfo info)Information about the display this Renderer is associated to. Renderer.DisplayInfogetDisplayInfo()Returns the DisplayInfo object set in setDisplayInfo or a new instance otherwise. voidsetFrameRateOptions(@NonNull() Renderer.FrameRateOptions options)Set options controlling the desired frame-rate. Renderer.FrameRateOptionsgetFrameRateOptions()Returns the FrameRateOptions object set in setFrameRateOptions or a new instanceotherwise. voidsetClearOptions(@NonNull() Renderer.ClearOptions options)Set ClearOptions which are used at the beginning of a frame to clear or retain theSwapChain content. Renderer.ClearOptionsgetClearOptions()Returns the ClearOptions object set in setClearOptions or a new instanceotherwise. EnginegetEngine()Gets the Engine that created this Renderer.voidsetPresentationTime(long monotonicClockNanos)Set the time at which the frame must be presented to the display. booleanbeginFrame(@NonNull() SwapChain swapChain, long frameTimeNanos)Sets up a frame for this Renderer.voidendFrame()Finishes the current frame and schedules it for display. voidrender(@NonNull() View view)Renders a View into this Renderer's window.voidrenderStandaloneView(@NonNull() View view)Renders a standalone View into its associated RenderTarget.voidcopyFrame(@NonNull() SwapChain dstSwapChain, @NonNull() Viewport dstViewport, @NonNull() Viewport srcViewport, int flags)Copies the currently rendered View to the indicated SwapChain, using theindicated source and destination rectangle. voidmirrorFrame(@NonNull() SwapChain dstSwapChain, @NonNull() Viewport dstViewport, @NonNull() Viewport srcViewport, int flags)voidreadPixels(@IntRange(from = 0) int xoffset, @IntRange(from = 0) int yoffset, @IntRange(from = 0) int width, @IntRange(from = 0) int height, @NonNull() Texture.PixelBufferDescriptor buffer)Reads back the content of the SwapChain associated with this Renderer.voidreadPixels(@NonNull() RenderTarget renderTarget, @IntRange(from = 0) int xoffset, @IntRange(from = 0) int yoffset, @IntRange(from = 0) int width, @IntRange(from = 0) int height, @NonNull() Texture.PixelBufferDescriptor buffer)Reads back the content of a specified RenderTarget. doublegetUserTime()Returns a timestamp (in seconds) for the last call to beginFrame. voidresetUserTime()Sets the user time epoch to now, i.e. longgetNativeObject()-
-
Method Detail
-
setDisplayInfo
void setDisplayInfo(@NonNull() Renderer.DisplayInfo info)
Information about the display this Renderer is associated to. This information is neededto accurately compute dynamic-resolution scaling and for frame-pacing.
-
getDisplayInfo
@NonNull() Renderer.DisplayInfo getDisplayInfo()
Returns the DisplayInfo object set in setDisplayInfo or a new instance otherwise.
-
setFrameRateOptions
void setFrameRateOptions(@NonNull() Renderer.FrameRateOptions options)
Set options controlling the desired frame-rate.
-
getFrameRateOptions
@NonNull() Renderer.FrameRateOptions getFrameRateOptions()
Returns the FrameRateOptions object set in setFrameRateOptions or a new instanceotherwise.
-
setClearOptions
void setClearOptions(@NonNull() Renderer.ClearOptions options)
Set ClearOptions which are used at the beginning of a frame to clear or retain theSwapChain content.
-
getClearOptions
@NonNull() Renderer.ClearOptions getClearOptions()
Returns the ClearOptions object set in setClearOptions or a new instanceotherwise.
-
setPresentationTime
void setPresentationTime(long monotonicClockNanos)
Set the time at which the frame must be presented to the display.
This must be called between beginFrame and endFrame.
- Parameters:
monotonicClockNanos- The time in nanoseconds corresponding to the system monotonicup-time clock.
-
beginFrame
boolean beginFrame(@NonNull() SwapChain swapChain, long frameTimeNanos)
Sets up a frame for this
Renderer.beginFramemanages frame pacing, and returns whether or not a frame should bedrawn. The goal of this is to skip frames when the GPU falls behind in order to keep the framelatency low.If a given frame takes too much time in the GPU, the CPU will get ahead of the GPU. Thedisplay will draw the same frame twice producing a stutter. At this point, the CPU isahead of the GPU and depending on how many frames are buffered, latency increases.beginFrame() attempts to detect this situation and returns
falsein that case,indicating to the caller to skip the current frame.All calls to render() must happen after beginFrame().
-
endFrame
void endFrame()
Finishes the current frame and schedules it for display.
endFrame()schedules the current frame to be displayed on theRenderer's window.All calls to render() must happen before endFrame().
-
render
void render(@NonNull() View view)
Renders a View into this
Renderer's window.This is filament's main rendering method, most of the CPU-side heavy lifting is performedhere. The purpose of the
render()function is to generate render commands whichare asynchronously executed by the Engine's render thread.render()generates commands for each of the following stages:- Shadow map passes, if needed
- Depth pre-pass
- SSAO pass, if enabled
- Color pass
- Post-processing pass
A typical render loop looks like this:
void renderLoop(Renderer renderer, SwapChain swapChain) { do { // typically we wait for VSYNC and user input events if (renderer.beginFrame(swapChain)) { renderer.render(mView); renderer.endFrame(); } } while (!quit()); }render()must be called afterbeginFrame and beforeendFrame.render()must be called from the Engine's main thread(or external synchronization must be provided). In particular, calls torender()on differentRendererinstances must be synchronized.render()performs potentially heavy computations and cannot be multi-threaded.However, internally, it is highly multi-threaded to both improve performance and mitigatethe call's latency.render()is typically called once per frame (but not necessarily).
- Parameters:
view- the View to render
-
renderStandaloneView
void renderStandaloneView(@NonNull() View view)
Renders a standalone View into its associated
RenderTarget.This call is mostly equivalent to calling render inside a beginFrame / endFrame block, but incurs less overhead. It can be usedas a poor man's compute API.
renderStandaloneView()must be called outside of beginFrame / endFrame.renderStandaloneView()must be called from the Engine's main thread(or external synchronization must be provided). In particular, calls torenderStandaloneView()on differentRendererinstances mustbe synchronized.renderStandaloneView()performs potentially heavy computations and cannot bemulti-threaded. However, internally, it is highly multi-threaded to both improve performanceand mitigate the call's latency.
- Parameters:
view- the View to render.
-
copyFrame
void copyFrame(@NonNull() SwapChain dstSwapChain, @NonNull() Viewport dstViewport, @NonNull() Viewport srcViewport, int flags)
Copies the currently rendered View to the indicated SwapChain, using theindicated source and destination rectangle.
copyFrame()should be called after a frame is rendered using render but before endFrame is called.- Parameters:
dstSwapChain- the SwapChain into which the frame should be copieddstViewport- the destination rectangle in which to draw the viewsrcViewport- the source rectangle to be copiedflags- one or moreCopyFrameFlagbehavior configuration flags
-
mirrorFrame
@Deprecated() void mirrorFrame(@NonNull() SwapChain dstSwapChain, @NonNull() Viewport dstViewport, @NonNull() Viewport srcViewport, int flags)
-
readPixels
void readPixels(@IntRange(from = 0) int xoffset, @IntRange(from = 0) int yoffset, @IntRange(from = 0) int width, @IntRange(from = 0) int height, @NonNull() Texture.PixelBufferDescriptor buffer)
Reads back the content of the SwapChain associated with this
Renderer.Framebuffer as seen on User buffer (PixelBufferDescriptor) screen +--------------------+ | | .stride .alignment | | ----------------------->--> | | O----------------------+--+ low addresses | | | | | | | w | | | .top | | | <---------> | | V | | | +---------+ | | +---------+ | | | | ^ | | ======> | | | | | | x | h| | | |.left| | | | +------>| v | | +---->| | | | | +.........+ | | +.........+ | | | ^ | | | | | y | | +----------------------+--+ high addresses O------------+-------+readPixelsmust be called within a frame, meaning after beginFrame and before endFrame. Typically,readPixelswill be called after render.After calling this method, the callback associated with
bufferwill be invoked on the main thread, indicating that the read-back has completed.Typically, this will happen after multiple calls to beginFrame, render, endFrame.readPixelsis intended for debugging and testing.It will impact performance significantly.- Parameters:
xoffset- left offset of the sub-region to read backyoffset- bottom offset of the sub-region to read backwidth- width of the sub-region to read backheight- height of the sub-region to read backbuffer- client-side buffer where the read-back will be writtenThe following format are always supported:RGBARGBA_INTEGERThe following types are always supported:UBYTEUINTINTFLOATOther combination of format/type may be supported.
-
readPixels
void readPixels(@NonNull() RenderTarget renderTarget, @IntRange(from = 0) int xoffset, @IntRange(from = 0) int yoffset, @IntRange(from = 0) int width, @IntRange(from = 0) int height, @NonNull() Texture.PixelBufferDescriptor buffer)
Reads back the content of a specified RenderTarget.
Framebuffer as seen on User buffer (PixelBufferDescriptor) screen +--------------------+ | | .stride .alignment | | ----------------------->--> | | O----------------------+--+ low addresses | | | | | | | w | | | .top | | | <---------> | | V | | | +---------+ | | +---------+ | | | | ^ | | ======> | | | | | | x | h| | | |.left| | | | +------>| v | | +---->| | | | | +.........+ | | +.........+ | | | ^ | | | | | y | | +----------------------+--+ high addresses O------------+-------+Typically
readPixelswill be called after render and before endFrame.After calling this method, the callback associated with
bufferwill be invoked on the main thread, indicating that the read-back has completed.Typically, this will happen after multiple calls to beginFrame, render, endFrame.OpenGL only: if issuing a
readPixelson a RenderTarget backed by a Texture that had data uploaded to it via setImage, the data returnedfromreadPixelswill be y-flipped with respect to the setImage call.readPixelsis intended for debugging and testing.It will impact performance significantly.- Parameters:
renderTarget- RenderTarget to read back fromxoffset- left offset of the sub-region to read backyoffset- bottom offset of the sub-region to read backwidth- width of the sub-region to read backheight- height of the sub-region to read backbuffer- client-side buffer where the read-back will be writtenThe following format are always supported:RGBARGBA_INTEGERThe following types are always supported:UBYTEUINTINTFLOATOther combination of format/type may be supported.
-
getUserTime
double getUserTime()
Returns a timestamp (in seconds) for the last call to beginFrame. This value isconstant for all views rendered during a frame. The epoch is set with resetUserTime.
In materials, this value can be queried using
vec4 getUserTime(). The valuereturned is ahighp vec4encoded as follows:
It follows that the following invariants are true:time.x = (float)Renderer.getUserTime(); time.y = Renderer.getUserTime() - time.x;
This encoding allows the shader code to perform high precision (i.e. double) timecalculations when needed despite the lack of double precision in the shader, e.g.:To compute(double)time.x + (double)time.y == Renderer.getUserTime() time.x == (float)Renderer.getUserTime()(double)time * vertexin the material, use the following construct:
Most of the time, high precision computations are not required, but be aware that theprecision ofvec3 result = time.x * vertex + time.y * vertex;time.xrapidly diminishes as time passes:In other words, it is only possible to get microsecond accuracy for about 16s or millisecondaccuracy for just under 5h. This problem can be mitigated by calling resetUserTime,or using high precision time as described above.
-
resetUserTime
void resetUserTime()
Sets the user time epoch to now, i.e. resets the user time to zero.
Use this method used to keep the precision of time high in materials, in practice it shouldbe called at least when the application is paused, e.g.
Activity.onPausein Android.
-
getNativeObject
long getNativeObject()
-
-
-
-