Class GraphicsDevice
The low level command surface of the 3D API, bound to a single RenderView
and its GPU context. A concrete subclass is provided by each platform
backend (OpenGL ES on Android, WebGL on the browser, Metal on iOS, desktop GL
on the simulator). Applications obtain the device from the Renderer
callbacks and never construct it directly.
The device owns shader generation and caching: when draw is called it looks
at the Material and the mesh VertexFormat, generates (once) the matching
platform shader, uploads any dirty buffers and issues the draw call.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanbeginPostEffects(PostProcess effects, int width, int height) The device's own post processing: redirects drawing into an offscreen image soendPostEffectscan apply the built-in effects ofeffects.final booleanbeginPostProcess(PostProcess effects, int width, int height) Redirects drawing into an offscreen image soendPostProcesscan applyeffectsto it on its way to the screen.booleanbeginRenderTarget(Texture target, int clearArgb) Sends the draws that follow intotargetuntilendRenderTarget, clearing its colour toclearArgband its depth first.booleanbeginShadowPass(Light light) Starts rendering the light's shadow map.abstract voidclear(int argbColor, boolean color, boolean depth) Clears the framebuffer.createIndexBuffer(int indexCount) Allocates an index buffer.createRenderTarget(int width, int height) Creates an offscreen image the scene can be drawn into withbeginRenderTargetand then sampled like any other texture: a mirror, a security camera screen, a minimap, the refraction behind glass.abstract TexturecreateTexture(int width, int height, int[] argb) Creates a GPU texture from raw ARGB pixel data.abstract TexturecreateTexture(Image image) Creates a GPU texture from a Codename One image.createVertexBuffer(VertexFormat format, int vertexCount) Allocates a vertex buffer.abstract voiddispose(IndexBuffer buffer) Releases the GPU resources backing an index buffer.abstract voidReleases the GPU resources backing a texture.abstract voiddispose(VertexBuffer buffer) Releases the GPU resources backing a vertex buffer.abstract voidDraws a mesh with the supplied material and model matrix.voiddrawInstanced(Mesh mesh, Material material, float[] matrices, int[] colors, int count) Drawscountcopies of a mesh, each with its own transform and, optionally, colour: crates along a quay, a field of grass, a flock.protected voidendPostEffects(float timeSeconds) Applies the effects passed tobeginPostEffectsand draws the result to the screen.final voidendPostProcess(float timeSeconds) Applies the effects passed tobeginPostProcessand draws the result to the screen.voidEnds the passbeginRenderTargetstarted; drawing returns to the target and viewport that were active before it.voidEnds the shadow pass started bybeginShadowPass, and restores the viewport and target that were active before it.Returns the active camera, or null if none was set.abstract GpuCapabilitiesReturns the capabilities and limits of the underlying GPU.Returns the active environment.getLight()Returns the active light.floatgetTime()Returns the clock set withsetTime.booleanTrue if this device skins meshes on the GPU: it draws a mesh inVertexFormat.SKINNEDwith a material givenMaterial.setSkinningby moving each vertex in its vertex stage.protected final booleanFor a backend: true once a shadow pass has ended since the camera was last set, so the map belongs to this frame.protected final voidpackDepthEffects(PostProcess effects, float[] out, int offset) Writes the parameters of the depth effects and the colour lookup intooutfromoffset, twelve floats: ambient occlusion strength and radius in pixels, focus distance, depth of field blur in pixels, outline strength, the camera's near and far planes, lookup strength, and the outline colour as red, green, blue (0 to 1) and a zero.protected final voidpackPostEffects(PostProcess effects, float[] out, int offset) Writes the parameters of the post effects added after grain intooutfromoffset: chromatic aberration, FXAA (0 or 1), god ray strength scaled by how visible the sun is, and the sun's position in texture space (0, 0 is the top left).voidSets the active camera supplying the view and projection matrices used by subsequent draws.voidsetEnvironment(Environment environment) Sets the environment (fog, hemisphere ambient, tone mapping) used by subsequent draws.voidSets the active directional light used by lit materials.voidsetTime(float seconds) Sets the clock that custom shaders read withcom.codename1.gpu.shader.ShaderNode.time(), in seconds.abstract voidsetViewport(int x, int y, int width, int height) Sets the viewport rectangle in pixels.protected final voidFor a backend: records that a shadow pass of this frame has ended.
-
Constructor Details
-
GraphicsDevice
public GraphicsDevice()
-
-
Method Details
-
getCapabilities
Returns the capabilities and limits of the underlying GPU. -
createVertexBuffer
Allocates a vertex buffer. The backing array is SIMD aligned so it can be uploaded to the GPU without an intermediate copy on ParparVM.
Parameters
-
format: the interleaved vertex layout -
vertexCount: the number of vertices
Returns
a new vertex buffer tracked by this device
-
-
createIndexBuffer
Allocates an index buffer.
Parameters
indexCount: the number of indices
Returns
a new index buffer tracked by this device
-
createTexture
-
createTexture
Creates a GPU texture from raw ARGB pixel data.
Parameters
-
width: the texture width in pixels -
height: the texture height in pixels -
argb:width * heightpacked ARGB pixels in row major order
Returns
a new texture
-
-
clear
public abstract void clear(int argbColor, boolean color, boolean depth) Clears the framebuffer.
Parameters
-
argbColor: the packed ARGB clear color -
color: true to clear the color buffer -
depth: true to clear the depth buffer
-
-
setViewport
public abstract void setViewport(int x, int y, int width, int height) Sets the viewport rectangle in pixels. -
setCamera
Sets the active camera supplying the view and projection matrices used by subsequent draws.
Setting the camera starts a new frame as far as shadows go: a shadow map rendered before it is not used again, so set the camera first and then run
beginShadowPass.Parameters
camera: the camera
-
isShadowMapCurrent
protected final boolean isShadowMapCurrent()For a backend: true once a shadow pass has ended since the camera was last set, so the map belongs to this frame. A frame that skipped the pass must not sample the one before it. -
shadowMapRendered
protected final void shadowMapRendered()For a backend: records that a shadow pass of this frame has ended. -
getCamera
Returns the active camera, or null if none was set. -
setLight
Sets the active directional light used by lit materials.
Parameters
light: the light
-
getLight
Returns the active light. -
setEnvironment
Sets the environment (fog, hemisphere ambient, tone mapping) used by subsequent draws.
Parameters
environment: the environment; null restores the default, which changes nothing
-
getEnvironment
Returns the active environment. -
setTime
public void setTime(float seconds) Sets the clock that custom shaders read with
com.codename1.gpu.shader.ShaderNode.time(), in seconds. Call it once per frame before drawing; animated library shaders such as water move with it.Parameters
seconds: the time
-
getTime
public float getTime()Returns the clock set with
setTime.Returns
seconds
-
beginShadowPass
Starts rendering the light's shadow map. Until
endShadowPass,drawwrites only depth as seen from the light, and skips materials that do not cast shadows. Draws afterendShadowPassthat receive shadows sample the map.The default implementation supports no shadows and returns false; the caller then skips the pass and draws as usual.
Render the shadow pass before
beginPostProcess, never inside it: both passes return to the target that was active when they began, so a shadow pass started offscreen would hand the post process its own image to draw into.Parameters
light: the light whose shadows to render; it must have shadows on
Returns
true if the pass started and the shadow casters should be drawn now
-
endShadowPass
public void endShadowPass()Ends the shadow pass started bybeginShadowPass, and restores the viewport and target that were active before it. -
createRenderTarget
Creates an offscreen image the scene can be drawn into with
beginRenderTargetand then sampled like any other texture: a mirror, a security camera screen, a minimap, the refraction behind glass.The result is an ordinary
Texture; hand it to a material or to aShaderMaterialtexture slot, and release it withdispose(Texture). It has a depth buffer of its own while it is drawn into.The default implementation cannot draw offscreen and returns null, as the software simulator device does.
Parameters
-
width: the width in pixels -
height: the height in pixels
Returns
the target, or null when this device has no offscreen drawing
-
-
isGpuSkinning
public boolean isGpuSkinning()True if this device skins meshes on the GPU: it draws a mesh in
VertexFormat.SKINNEDwith a material givenMaterial.setSkinningby moving each vertex in its vertex stage. Where it answers false, as the software simulator device does, skin on the CPU and draw the result as an ordinary mesh;com.codename1.gaming.AnimatedModelpicks the path by itself.Returns
false by default
-
beginRenderTarget
Sends the draws that follow into
targetuntilendRenderTarget, clearing its colour toclearArgband its depth first. The viewport covers the whole target, and the camera, light and environment are the device's as usual, so set a camera for the view the target shows.Draw render targets first in a frame: before the shadow pass, before
beginPostProcessand before the scene itself. They do not nest, and on some devices the depth of scene draws made earlier in the frame is not kept across one.Parameters
-
target: a texture fromcreateRenderTargeton this device -
clearArgb: the colour the target starts from
Returns
true if drawing now goes into the target
-
-
endRenderTarget
public void endRenderTarget()Ends the passbeginRenderTargetstarted; drawing returns to the target and viewport that were active before it. -
beginPostProcess
Redirects drawing into an offscreen image so
endPostProcesscan applyeffectsto it on its way to the screen. Wheneffectshas custom passes (PostProcess.addPass) and this device can draw into a render target, the scene goes into one, andendPostProcessruns the passes before the built-in effects.Draw the shadow pass and any render targets before this call.
Parameters
-
effects: the effects to apply -
width: the width of the frame in pixels -
height: the height of the frame in pixels
Returns
true if drawing now goes offscreen; false means the caller draws straight to the screen
-
-
endPostProcess
public final void endPostProcess(float timeSeconds) Applies the effects passed to
beginPostProcessand draws the result to the screen. Drawing goes to the screen again afterwards.Parameters
timeSeconds: a running clock, which animates the film grain
-
beginPostEffects
The device's own post processing: redirects drawing into an offscreen image so
endPostEffectscan apply the built-in effects ofeffects. Custom passes are handled bybeginPostProcessand never reach this.The default implementation supports no post processing and returns false; the caller then draws straight to the screen.
Parameters
-
effects: the effects to apply -
width: the width of the frame in pixels -
height: the height of the frame in pixels
Returns
true if drawing now goes offscreen
-
-
endPostEffects
protected void endPostEffects(float timeSeconds) Applies the effects passed to
beginPostEffectsand draws the result to the screen.Parameters
timeSeconds: a running clock, which animates the film grain
-
draw
Draws a mesh with the supplied material and model matrix. The device composes
camera.getViewProjection() * modelMatrix, binds the generated shader for the material, applies the material render state and issues the draw call.Parameters
-
mesh: the geometry to draw -
material: how to shade the geometry -
modelMatrix: the 16 element column-major model transform, or null for the identity
-
-
drawInstanced
Draws
countcopies of a mesh, each with its own transform and, optionally, colour: crates along a quay, a field of grass, a flock. The material's shader tells the copies apart withShaderNode.instanceId,instanceRandomandinstanceColor, so one material varies size, tint and motion across thousands of copies.Every device draws the copies one after another from a single compiled pipeline, changing only the transform and the per copy uniforms between them, which is what makes it cheaper than a model per copy: no state is looked up or rebound and the scene walks one node.
Parameters
-
mesh: the mesh -
material: the material shared by every copy -
matrices: 16 column-major floats per copy -
colors: an ARGB colour per copy, or null for white -
count: how many copies to draw
-
-
dispose
Releases the GPU resources backing a vertex buffer. -
dispose
Releases the GPU resources backing an index buffer. -
dispose
Releases the GPU resources backing a texture. -
packPostEffects
Writes the parameters of the post effects added after grain into
outfromoffset: chromatic aberration, FXAA (0 or 1), god ray strength scaled by how visible the sun is, and the sun's position in texture space (0, 0 is the top left). Every device packs them in this order, so the composite passes of every shading language agree.Parameters
-
effects: the post process being composited -
out: the parameter array -
offset: where to write the six floats
-
-
packDepthEffects
Writes the parameters of the depth effects and the colour lookup into
outfromoffset, twelve floats: ambient occlusion strength and radius in pixels, focus distance, depth of field blur in pixels, outline strength, the camera's near and far planes, lookup strength, and the outline colour as red, green, blue (0 to 1) and a zero. Every device packs them in this order, so the composite passes of every shading language agree.Parameters
-
effects: the post process being composited -
out: the parameter array -
offset: where to write the twelve floats
-
-