|
| |
Java 3D Performance Guide for J3D 1.3
Sourced from java3d-interest list. Posted by
Chien Yang of Sun
The Java 3D API was designed with high performance 3D graphics
as a primary goal. This document presents the performance features of
Java 3D in a number of ways. It describes the specific APIs that were
included for performance. It describes which optimizations are currently
implemented in Java 3D 1.3. And, it describes a number of tips and tricks
that application writers can use to improve the performance of their
application.
Performance in the API
There are a number of things in the API that were included specifically
to increase performance. This section examines a few of them.
Tips and Tricks
This section presents a number of tips and tricks for an application
programmer to try when optimizing their application. These tips focus on
improving rendering frame rates, but some may also help overall application
performance.
- Move Object vs. Move ViewPlatform
If the application simply needs to transform the entire scene,
transform the ViewPlatform instead. This changes the problem
from transforming every object in the scene into only
transforming the ViewPlatform.
- Capability bits
Only set them when needed. Many optimizations can be done
when they are not set. So, plan out application requirements
and only set the capability bits that are needed.
- Bounds and Activation Radius
Consider the spatial extent of various leaf nodes in the scene
and assign bounds accordingly. This allows the implementation
to prune processing on objects that are not in close
proximity. Note, this does not apply to Geometric bounds.
Automatic bounds calculations for geometric objects is fine.
In cases such as the influencing or scheduling bounds
encompass the entire scene graph, setting this bounds to
infinite bounds may help improve performance. Java3d will
shortcircuit intersection test on bounds with infinite
volume. A BoundingSphere is a infinite bounds if it's radius
is set to Double.POSITIVE_INFINITY. A BoundingBox is a
infinite bounds if it's lower(x, y, z) are set to
Double.NEGATIVE_INFINITY, and it's upper(x, y, z) are set
Double.POSITIVE_INFINITY.
Bounds computation does consume CPU cycles. If an application
does a lot of geometry coordinate updates, to improve
performance, it is better to turn off auto bounds compute. The
application will have to do the bounds update itself.
- Change Number of Shape3D Nodes
In the current implementation there is a certain amount of
fixed overhead associated with the use of the Shape3D node.
In general, the fewer Shape3D nodes that an application uses,
the better. However, combining Shape3D nodes without
factoring in the spatial locality of the nodes to be combined
can adversely effect performance by effectively disabling view
frustum culling. An application programmer will need to
experiment to find the right balance of combining Shape3D
nodes while leveraging view frustum culling. The .compile
optimization that combines shape node will do this
automatically, when possible.
- Geometry Type and Format
Most rendering hardware reaches peak performance when
rendering long triangle strips. Unfortunately, most geometry
data stored in files is organized as independent triangles or
small triangle fans (polygons). The Java 3D utility package
includes a stripifier utility that will try to convert a given
geometry type into long triangle strips. Application
programmers should experiment with the stripifier to see if it
helps with their specific data. If not, any stripification
that the application can do will help. Another option is that
most rendering hardware can process a long list of independent
triangles faster than a long list of single triangle triangle
fans. The stripifier in the Java 3D utility package will be
continually updated to provided better stripification.
- Sharing Appearance/Texture/Material NodeComponents
To assist the implementation in efficient state sorting, and
allow more shape nodes to be combined during compilation,
applications can help by sharing Appearance/Texture/Material
NodeComponent objects when possible.
- Geometry by reference
Using geometry by reference reduces the memory needed to store
a scene graph, since Java 3D avoids creating a copy in some
cases. However, using this features prevents Java 3D from
creating display lists (unless the scene graph is compiled),
so rendering performance can suffer in some cases. It is
appropriate if memory is a concern or if the geometry is
writable and may change frequently. The interleaved format
will perform better than the non-interleaved formats, and
should be used where possible. In by-reference mode, an
application should use arrays of native data types; referring
to TupleXX[] arrays should be avoided.
See also the Space versus time property tradeoff in the previous section.
- Texture by reference and Y-up
Using texture by reference and Y-up format may reduce the
memory needed to store a texture object, since Java 3D avoids
creating a copy in some cases. When a copy of the by-reference
data is made in Java3D, users should be aware that this case
will use twice as much memory as the by copy case. This is due
to the fact that Java3D internally makes a copy in addition to
the user's copy to the reference data. Currently, Java3D will not
make a copy of texture image for the following combinations of
BufferedImage format and ImageComponent format (byReference
and Yup should both be set to true):
On both Solaris and Win32 OpenGL:
BufferedImage.TYPE_CUSTOM ImageComponent.FORMAT_RGB8 or
of form 3BYTE_RGB ImageComponent.FORMAT_RGB
BufferedImage.TYPE_CUSTOM ImageComponent.FORMAT_RGBA8 or
of form 4BYTE_RGBA ImageComponent.FORMAT_RGBA
BufferedImage.TYPE_BYTE_GRAY ImageComponent.FORMAT_CHANNEL8
On Win32/OpenGL:
BufferedImage format ImageComponentFormat
---------------------- ----------------------
BufferedImage.TYPE_3BYTE_BGR ImageComponent.FORMAT_RGB8 or
ImageComponent.FORMAT_RGB
On Solaris/OpenGL:
BufferedImage format ImageComponentFormat
---------------------- ----------------------
BufferedImage.TYPE_4BYTE_ABGR ImageComponent.FORMAT_RGBA8 or
ImageComponent.FORMAT_RGBA
-
Drawing 2D graphics using J3DGraphics2D
The J3DGraphics2D class allows you to mix 2D and 3D drawing
into the same window. However, this can be very slow in many
cases because Java 3D needs to buffer up all of the data and
then composite it into the back buffer of the Canvas3D. A new
method, drawAndFlushImage, is provided to accelerate the
drawing of 2D images into a Canvas3D. To use this, it is
recommended that an application create their own BufferedImage
of the desired size, use Java2D to render into their
BufferedImage, and then use the new drawAndFlushImage method
to draw the image into the Canvas3D.
This has the advantage of only compositing the minimum area
and, in some cases, can be done without making an extra copy
of the data. For the image to not be copied, this method must
be called within a Canvas3D callback, the specified
BufferedImage must be of the format
BufferedImage.TYPE_4BYTE_ABGR, and the GL_ABGR_EXT extension
must be supported by OpenGL. If these conditions are not met,
the image will be copied, and then flushed.
The following methods have also been optimized : all drawImage()
routines, drawRenderableImage(), draw(Shape s), fill(Shape s),
drawString(), drawLine() without strokeSet to copy only the
minimum affected region without the restriction imposed in
drawAndFlushImage method.
- Application Threads
The built in threads support in the Java language is very
powerful, but can be deadly to performance if it is not
controlled. Applications need to be very careful in their
threads usage. There are a few things to be careful of when
using Java threads. First, try to use them in a demand driven
fashion. Only let the thread run when it has a task to do.
Free running threads can take a lot of cpu cycles from the
rest of the threads in the system - including Java 3D threads.
Next, be sure the priority of the threads are appropriate.
Most Java Virtual Machines will enforce priorities
aggressively. Too low a priority will starve the thread and
too high a priority will starve the rest of the system. If in
doubt, use the default thread priority. Finally, see if the
application thread really needs to be a thread. Would the
task that the thread performs be all right if it only ran once
per frame? If so, consider changing the task to a Behavior
that wakes up each frame.
- Java 3D Threads
Java 3D uses many threads in its implementation, so it also
needs to implement the precautions listed above. In almost
all cases, Java 3D manages its threads efficiently. They are
demand driven with default priorities. There are a few cases
that don't follow these guidelines completely.
Behaviors
One of these cases is the Behavior scheduler when there
are pending WakeupOnElapsedTime criteria. In this case,
it needs to wakeup when the minimum WakeupOnElapsedTime
criteria is about to expire. So, application use of
WakeupOnElapsedTime can cause the Behavior scheduler to
run more often than might be necessary.
Sounds
The final special case for Java 3D threads is the Sound
subsystem. Due to some limitations in the current sound
rendering engine, enabling sounds cause the sound engine
to potentially run at a higher priority than other
threads. This may adversely effect performance.
There is one last comment to make on threads in general.
Since Java 3D is a fully multithreaded system, applications
may see significant performance improvements by increasing the
number of CPUs in the system. For an application that does
strictly animation, then two CPUs should be sufficient. As
more features are added to the application (Sound, Collision,
etc.), more CPUs could be utilized.
- Switch Nodes for Occlusion Culling
If the application is a first person point of view
application, and the environment is well known, Switch nodes
may be used to implement simple occlusion culling. The
children of the switch node that are not currently visible may
be turned off. If the application has this kind of knowledge,
this can be a very useful technique.
- Switch Nodes for Animation
Most animation is accomplished by changing the transformations
that effect an object. If the animation is fairly simple and
repeatable, the flip-book trick can be used to display the
animation. Simply put all the animation frames under one
switch node and use a SwitchValueInterpolator on the switch
node. This increases memory consumption in favor of smooth
animations.
- OrderedGroup Nodes
OrderedGroup and its subclasses are not as high performing as
the unordered group nodes. They disable any state sorting
optimizations that are possible. If the application can find
alternative solutions, performance will improve.
- LOD Behaviors
For complex scenes, using LOD Behaviors can improve
performance by reducing geometry needed to render objects that
don't need high level of detail. This is another option that
increases memory consumption for faster render rates.
- Picking
If the application doesn't need the accuracy of geometry based
picking, use bounds based picking. For more accurate picking
and better picking performance, use PickRay instead of
PickCone/PickCylnder unless you need to pick line/point.
PickCanvas with a tolerance of 0 will use PickRay for picking.
- D3D users only
Using Quad with Polygon line mode is very slow. This is because
DirectX doesn't support Quad. Breaking down the Quad
into two triangles causes the the diagonal line to be displayed.
Instead Java 3D draws the polygon line and does the hidden surface
removal manually.
Automatic texture generation mode Eye Linear is slower
because D3D doesn't support this mode.
|