Hardware Shaders
These are coding tips for hardware shader plug-in writers.
- Do not push and pop all GL attributes if not required. This is more expensive than pushing specific attributes.
- If you’re only using hardware shaders in the scene view, we recommend that you use the older API: geometry(), bind() and unbind().
If you also want to batch render your hardware shader, we recommend you use the newer API: glGeometry, glBind and glUnbind.
- We recommend you cache all node attributes as internal. The example plug-ins hwColorPerVertex, hwPhongShader and the cgFxShader
all do this. Non-cached values are very expensive to evaluate and can double the draw time.
- Avoid using complex attributes (structures and arrays).
- If the output color on the shader is not important, do not make any attributes affect it. This causes additional computation
as marking one attribute dirty causes a recomputation to derive the dependent one.
- The compute() method can be left empty if not required. The simpler the method the better. You will not see swatches if you do not compute the output color.
- Do not post-normalize tangents in the plug-in. This is done for you.
- Data sent via the geometry calls are read-only and are cached internally. Do not modify the values.
- Use simple data structures (versus Maya API objects) for simple data. For example, use float3 versus MFloatVector. There is
a performance overhead due to the interfaces used in OpenMaya.
- Take advantage of methods available on M3dView. OpenGL state is cached internally and can give better performance than using
beginGL(), endGL() and direct calls to OpenGL.
- Use glDrawRangeElements to draw with. This is the API recommended by the card vendor for drawing with the geometry arrays
passed to the hardware shader.
- Make sure to set the hasTransparency() return value appropriately. Setting it to true causes Maya to draw an object twice
(once culling front faces, once culling back).
- Take into account the current display state in M3dView. For example, don't disable lighting if the display mode is .
- When transparency is enabled, framebuffer blending is already enabled. You do not need to enable it again.
- Take advantage of the method: MPxHwShaderNode::currentPath() to send the appropriate information for attribute query methods
(for example, getTexCoordSetNames()).
- Test which parameters on color/alpha and depth mask are enabled for interactive and especially for hardware rendering. They
can give hints as to how to draw a simpler version of the geometry.
- For the Hardware renderer, the plug-in can be called multiple times. The general sequence is usually: depth pass, [lighting
pass[es]], color pass, [shadow map pass], [alpha pass], [depth pass]. Items marked with braces “[“ “]” are optional and dependent
upon the number of lights in the scene, whether shadows are enabled for those lights, and whether alpha and depth output images
are specified in the Render Settings.
- Use the option which is available per object (NURBS or polygonal surfaces). This is available in the in the section and as an option in the menu. Use to make the hardware shader not show up on the object, or use to restore the default state of showing the shader. This allows users of the plug-in to selectively disable shader display
for performance reasons.
Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License