Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public abstract class GraphicsDevice
- Object
- 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.
Constructors
public GraphicsDevice() |
Methods
public abstract GpuCapabilities getCapabilities() | Returns the capabilities and limits of the underlying GPU. |
public VertexBuffer createVertexBuffer(VertexFormat format, int vertexCount) | Allocates a vertex buffer. |
public IndexBuffer createIndexBuffer(int indexCount) | Allocates an index buffer. |
public abstract Texture createTexture(Image image) | Creates a GPU texture from a Codename One image. |
public abstract Texture createTexture(int width, int height, int[] argb) | Creates a GPU texture from raw ARGB pixel data. |
public abstract void clear(int argbColor, boolean color, boolean depth) | Clears the framebuffer. |
public abstract void setViewport(int x, int y, int width, int height) | Sets the viewport rectangle in pixels. |
public void setCamera(Camera camera) | Sets the active camera supplying the view and projection matrices used by subsequent draws. |
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. |
protected final void shadowMapRendered() | For a backend: records that a shadow pass of this frame has ended. |
public Camera getCamera() | Returns the active camera, or null if none was set. |
public void setLight(Light light) | Sets the active directional light used by lit materials. |
public Light getLight() | Returns the active light. |
public void setEnvironment(Environment environment) | Sets the environment (fog, hemisphere ambient, tone mapping) used by subsequent draws. |
public Environment getEnvironment() | Returns the active environment. |
public void setTime(float seconds) | Sets the clock that custom shaders read with com.codename1.gpu.shader.ShaderNode.time(), in seconds. |
public float getTime() | Returns the clock set with setTime. |
public boolean beginShadowPass(Light light) | Starts rendering the light’s shadow map. |
public void endShadowPass() | Ends the shadow pass started by beginShadowPass, and restores the viewport and target that were active before it. |
public Texture createRenderTarget(int width, int height) | Creates an offscreen image the scene can be drawn into with beginRenderTarget and then sampled like any other texture: a mirror, a security camera screen, a minimap, the refraction behind glass. |
public boolean isGpuSkinning() | True if this device skins meshes on the GPU: it draws a mesh in VertexFormat.SKINNED with a material given Material.setSkinning by moving each vertex in its vertex stage. |
public boolean beginRenderTarget(Texture target, int clearArgb) | Sends the draws that follow into target until endRenderTarget, clearing its colour to clearArgb and its depth first. |
public void endRenderTarget() | Ends the pass beginRenderTarget started; drawing returns to the target and viewport that were active before it. |
public final boolean beginPostProcess(PostProcess effects, int width, int height) | Redirects drawing into an offscreen image so endPostProcess can apply effects to it on its way to the screen. |
public final void endPostProcess(float timeSeconds) | Applies the effects passed to beginPostProcess and draws the result to the screen. |
protected boolean beginPostEffects(PostProcess effects, int width, int height) | The device’s own post processing: redirects drawing into an offscreen image so endPostEffects can apply the built-in effects of effects. |
protected void endPostEffects(float timeSeconds) | Applies the effects passed to beginPostEffects and draws the result to the screen. |
public abstract void draw(Mesh mesh, Material material, float[] modelMatrix) | Draws a mesh with the supplied material and model matrix. |
public void drawInstanced(Mesh mesh, Material material, float[] matrices, int[] colors, int count) | Draws count copies of a mesh, each with its own transform and, optionally, colour: crates along a quay, a field of grass, a flock. |
public abstract void dispose(VertexBuffer buffer) | Releases the GPU resources backing a vertex buffer. |
public abstract void dispose(IndexBuffer buffer) | Releases the GPU resources backing an index buffer. |
public abstract void dispose(Texture texture) | Releases the GPU resources backing a texture. |
protected final void packPostEffects(PostProcess effects, float[] out, int offset) | Writes the parameters of the post effects added after grain into out from offset: 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). |
protected final void packDepthEffects(PostProcess effects, float[] out, int offset) | Writes the parameters of the depth effects and the colour lookup into out from offset, 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. |
Inherited methods
Constructor details
GraphicsDevice
public GraphicsDevice()Method details
getCapabilities
public abstract GpuCapabilities getCapabilities()createVertexBuffer
public VertexBuffer createVertexBuffer(VertexFormat format, int vertexCount)Parameters
formatVertexFormat- the interleaved vertex layout
vertexCountint- the number of vertices
Returns
createIndexBuffer
public IndexBuffer createIndexBuffer(int indexCount)Parameters
indexCountint- the number of indices
Returns
createTexture
public abstract Texture createTexture(Image image)Parameters
imageImage- the source image
Returns
createTexture
public abstract Texture createTexture(int width, int height, int[] argb)Parameters
widthint- the texture width in pixels
heightint- the texture height in pixels
argbint[]width * heightpacked ARGB pixels in row major order
Returns
clear
public abstract void clear(int argbColor, boolean color, boolean depth)Parameters
argbColorint- the packed ARGB clear color
colorboolean- true to clear the color buffer
depthboolean- true to clear the depth buffer
setViewport
public abstract void setViewport(int x, int y, int width, int height)setCamera
public void setCamera(Camera camera)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
cameraCamera- the camera
isShadowMapCurrent
protected final boolean isShadowMapCurrent()shadowMapRendered
protected final void shadowMapRendered()getCamera
public Camera getCamera()setLight
public void setLight(Light light)Parameters
lightLight- the light
getLight
public Light getLight()setEnvironment
public void setEnvironment(Environment environment)Parameters
environmentEnvironment- the environment; null restores the default, which changes nothing
getEnvironment
public Environment getEnvironment()setTime
public void setTime(float seconds)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
secondsfloat- the time
getTime
public float getTime()setTime.Returns
beginShadowPass
public boolean beginShadowPass(Light light)Starts rendering the light’s shadow map. Until endShadowPass, draw
writes only depth as seen from the light, and skips materials that do
not cast shadows. Draws after endShadowPass that 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
lightLight- the light whose shadows to render; it must have shadows on
Returns
endShadowPass
public void endShadowPass()beginShadowPass, and restores the
viewport and target that were active before it.createRenderTarget
public Texture createRenderTarget(int width, int height)Creates an offscreen image the scene can be drawn into with
beginRenderTarget and 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 a
ShaderMaterial texture slot, and release it with dispose(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
widthint- the width in pixels
heightint- the height in pixels
Returns
isGpuSkinning
public boolean isGpuSkinning()VertexFormat.SKINNED with a material given Material.setSkinning
by 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.AnimatedModel
picks the path by itself.Returns
beginRenderTarget
public boolean beginRenderTarget(Texture target, int clearArgb)Sends the draws that follow into target until endRenderTarget,
clearing its colour to clearArgb and 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
beginPostProcess and 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
targetTexture- a texture from
createRenderTargeton this device clearArgbint- the colour the target starts from
Returns
endRenderTarget
public void endRenderTarget()beginRenderTarget started; drawing returns to the
target and viewport that were active before it.beginPostProcess
public final boolean beginPostProcess(PostProcess effects, int width, int height)Redirects drawing into an offscreen image so endPostProcess can apply
effects to it on its way to the screen. When effects has custom
passes (PostProcess.addPass) and this device can draw into a render
target, the scene goes into one, and endPostProcess runs the passes
before the built-in effects.
Draw the shadow pass and any render targets before this call.
Parameters
effectsPostProcess- the effects to apply
widthint- the width of the frame in pixels
heightint- the height of the frame in pixels
Returns
endPostProcess
public final void endPostProcess(float timeSeconds)beginPostProcess and draws the result
to the screen. Drawing goes to the screen again afterwards.Parameters
timeSecondsfloat- a running clock, which animates the film grain
beginPostEffects
protected boolean beginPostEffects(PostProcess effects, int width, int height)The device’s own post processing: redirects drawing into an offscreen
image so endPostEffects can apply the built-in effects of effects.
Custom passes are handled by beginPostProcess and never reach this.
The default implementation supports no post processing and returns false; the caller then draws straight to the screen.
Parameters
effectsPostProcess- the effects to apply
widthint- the width of the frame in pixels
heightint- the height of the frame in pixels
Returns
endPostEffects
protected void endPostEffects(float timeSeconds)beginPostEffects and draws the result
to the screen.Parameters
timeSecondsfloat- a running clock, which animates the film grain
draw
public abstract void draw(Mesh mesh, Material material, float[] modelMatrix)camera.getViewProjection() * modelMatrix, binds the generated
shader for the material, applies the material render state and issues the
draw call.Parameters
meshMesh- the geometry to draw
materialMaterial- how to shade the geometry
modelMatrixfloat[]- the 16 element column-major model transform, or null for the identity
drawInstanced
public void drawInstanced(Mesh mesh, Material material, float[] matrices, int[] colors, int count)Draws count copies 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 with
ShaderNode.instanceId, instanceRandom and instanceColor, 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
meshMesh- the mesh
materialMaterial- the material shared by every copy
matricesfloat[]- 16 column-major floats per copy
colorsint[]- an ARGB colour per copy, or null for white
countint- how many copies to draw
dispose
public abstract void dispose(VertexBuffer buffer)dispose
public abstract void dispose(IndexBuffer buffer)dispose
public abstract void dispose(Texture texture)packPostEffects
protected final void packPostEffects(PostProcess effects, float[] out, int offset)out from offset: 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
effectsPostProcess- the post process being composited
outfloat[]- the parameter array
offsetint- where to write the six floats
packDepthEffects
protected final void packDepthEffects(PostProcess effects, float[] out, int offset)out from offset, 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
effectsPostProcess- the post process being composited
outfloat[]- the parameter array
offsetint- where to write the twelve floats