Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes

MPxHardwareShader Class Reference

This reference page is linked to from the following overview topics: Scene Overrides, Transition Guide, Removal of MStatus return codes from .NET API.


Search for all occurrences

Detailed Description

Base class for user defined hardware shaders.

MPxHardwareShader allows the creation of user-defined hardware shaders. A hardware shader allows the plug-in writer to control the on-screen display of an object in Maya.

You must derive a hardware shader node from MPxHardwareShader for it to have any affect on the on-screen display at all. In addition to their affect on the on-screen display of objects, hardware shaders function as surface shader nodes. This allows you to connect any shading network up to the hardware shader's outColor attribute to handle the shading during a software render.

To create a working hardware shader, derive from this class and override the render() and optionally the populateRequirements() methods. The other methods of the parent class MPxNode may also be overridden to perform dependency node capabilities.

NOTE: Plug-in hardware shaders are fully supported for polygonal mesh shapes. NURBS surfaces are only supported in the High Quality Interactive viewport and Hardware Renderer.

Examples:

D3DViewportRenderer.cpp, and hlslShader.h.

#include <MPxHardwareShader.h>

Inheritance diagram for MPxHardwareShader:
Inheritance graph
[legend]

List of all members.

Classes

struct  RenderParameters
 Provides information on how to render the image. More...
struct  ShaderContext
 Provides contextual information about the current invocation of the shader. More...

Public Types

enum  TransparencyOptions { kIsTransparent = 0x0001, kNoTransparencyFrontBackCull = 0x0002, kNoTransparencyPolygonSort = 0x0004 }
 

Transparency option bitmasks.

More...

Public Member Functions

 MPxHardwareShader ()
 Class constructor.
virtual ~MPxHardwareShader ()
 Class destructor.
virtual MPxNode::Type type () const
 This method returns the type of the node.
MStatus setVaryingParameters (const MVaryingParameterList &parameters, bool remapCurrentValues=true, MDagModifier *dagModifier=NULL)
 Call this method to set the list of varying parameters this shader uses.
MStatus setUniformParameters (const MUniformParameterList &parameters, bool remapCurrentValues=true, MDagModifier *dagModifier=NULL)
 Call this method to set the list of uniform parameters this shader uses.
virtual MStatus render (MGeometryList &iterator)
 Override this method to render geometry using your hardware shader.
virtual unsigned int transparencyOptions ()
 This method returns transparency options for usage as hints for Maya's internal draw during a given rendering pass.
virtual const MRenderProfileprofile ()
 Override this method to specify the renderers your shader supports.
virtual MStatus populateRequirements (const MPxHardwareShader::ShaderContext &context, MGeometryRequirements &requirements)
 This method is not available in Python.
virtual MStatus getAvailableImages (const MPxHardwareShader::ShaderContext &context, const MString &uvSetName, MStringArray &imageNames)
 This method is not available in Python.
virtual MStatus renderImage (const MPxHardwareShader::ShaderContext &context, const MString &imageName, floatRegion region, int &imageWidth, int &imageHeight)
 This method is not available in Python.
virtual MStatus renderImage (const MPxHardwareShader::ShaderContext &context, const MString &imageName, floatRegion region, const MPxHardwareShader::RenderParameters &parameters, int &imageWidth, int &imageHeight)
 This method is not available in Python.
virtual MStatus renderSwatchImage (MImage &image)
 If the shader specifies to override swatch rendering, then this method must be overridden in order to draw anything into a swatch.

Static Public Member Functions

static MPxHardwareShadergetHardwareShaderPtr (MObject &object)
 This is a static convenience method to be able to get an MPxHardwareShader from an MObject provided by a swatch generator class (Class derived from MSwatchRenderRegister).
static MString findResource (const MString &name, const MString &shaderPath, MStatus *status=NULL)
 This is a static utility to find the full path to a shader resource (typically a texture).
static const char * className ()
 Returns the name of this class.

Static Public Attributes

static MObject outColor
 output color value
static MObject outColorR
 output color red
static MObject outColorG
 output color green
static MObject outColorB
 output color blue

Member Enumeration Documentation

Transparency option bitmasks.

Enumerator:
kIsTransparent 

When set means draw transparent.

kNoTransparencyFrontBackCull 

When set means ignore front back cull.

kNoTransparencyPolygonSort 

When set means ignore polygon sorting.


Member Function Documentation

MPxNode::Type type ( ) const [virtual]

This method returns the type of the node.

This method should not be overridden by the user. It will return MPxNode::kHwShaderNode.

Returns:
The type of node

Reimplemented from MPxNode.

MStatus setVaryingParameters ( const MVaryingParameterList parameters,
bool  remapCurrentValues = true,
MDagModifier dagModifier = NULL 
)

Call this method to set the list of varying parameters this shader uses.

Once set, you can use these parameters directly to access geometry data for surfaces being shaded. When using this method to manage shader varying parameters, there is no need to override populateRequirements or handle the node interface as Maya will handle parameter setup, presentation and configuration for you.

It is important to call this method whenever the shader parameters are modified (including at load time).

This is an optional method - shader implementations are still free to manage geometry parameters independently if they wish.

Parameters:
[in]parametersthe list of varying parameters for this shader
[in]remapCurrentValuesif true (the default), Maya will attempt to initialise the value of new parameters based on any equivalently named parameters that currently exist on the node. Otherwise, the parameters will be setup using default values. Unless you wish to forcibly reset parameter values, the default value of true should be used.
[in]dagModifieran optional DG modifier to use when managing the attributes used to represent the geometry parameters on this shader.
Returns:
Status code
Status Codes:
MStatus setUniformParameters ( const MUniformParameterList parameters,
bool  remapCurrentValues = true,
MDagModifier dagModifier = NULL 
)

Call this method to set the list of uniform parameters this shader uses.

Once set, you can use these parameters to access the cached values of shader parameters, including testing when the value has been updated (to minimise the shader state changes). When using this method to manage uniform parameters, Maya will handle the underlyintg attributes, serialization and user interface for you.

It is important to call this method whenever the shader parameters are modified (including at load time).

This is an optional method - shader implementations are still free to manage uniform (i.e. shader-level) parameters independently if they wish.

Parameters:
[in]parametersthe list of uniform parameters for this shader
[in]remapCurrentValuesif true (the default), Maya will attempt to initialise the value of new parameters based on any equivalently named parameters that currently exist on the node. Otherwise, the parameters will be setup using default values. Unless you wish to forcibly reset parameter values, the default value of true should be used.
[in]dagModifieran optional DG modifier to use when managing the attributes used to represent the geometry parameters on this shader.
Returns:
Status code
Status Codes:
MStatus render ( MGeometryList iterator) [virtual]

Override this method to render geometry using your hardware shader.

It is important to note that you can only access surface data (e.g. uv sets) that you specified in populateRequirements. Any data you did not ask for is unlikely to be available in the geometry cache passed to you.

For example:

        for( ; !iter.isDone(); iter.next())
        {
            MGeometry& geometry = iter.geometry( MGeometryList::kMatrices | MGeometryList::kCulling);
            glVertexPointer( 3, GL_FLOAT, 0, geometry.position().data());
            glNormalPointer( GL_FLOAT, 0, geometry.normal().data());
            MGeometryPrimitive primitives = geometry.primitiveArray( 0);
            glDrawElements( GL_TRIANGLES, primitives.elementCount(), GL_UNSIGNED_INT, primitives.data());
        }
Parameters:
[in]iteratoran iterator containing the geometry items you need to render.
Returns:
Status code
Status Codes:
Examples:
hlslShader.h.
unsigned int transparencyOptions ( ) [virtual]

This method returns transparency options for usage as hints for Maya's internal draw during a given rendering pass.

Parameters are returned via an integer containing masked out bits. By default the mask is set to 0, meaning that the drawing should be treated as regular opaque object drawing. This will generally mean one call per draw pass.

Options to control transparency are specified by returning one or more masks specified by the TransparencyOptions enumeration :

  • kIsTransparent : Draw as a transparent object. If no transparency overrides are specified, then control of how to draw during a given pass is determined internally by Maya's refresh algorithm, and options the user can set per modelling viewport.
  • kNoTransparencyFrontBackCull : When kisTransparent is set and this flag is set, do not perform transparency drawing using the internal 2-pass front-face + back-face culling algorithm.
  • kNoTransparencyPolygonSort : When kisTransparent is set and this flag is set, do not perform transparency drawing using the internal 2-pass drawing of back-to-front sorted triangles.

Note : Setting the "hasTransparency()" method to true will override this method. This is for backward compatibility with behaviour on existing hardware shader nodes. It is recommended that shaders use the "transparencyOptions()" override, and not longer use the older "hasTransparency()" override from their shader classes.

Returns:
Integer containing the appropriate options set via masks.
Examples:
hlslShader.h.
const MRenderProfile & profile ( ) [virtual]

Override this method to specify the renderers your shader supports.

If this method is not overridden, Maya will assume your shader supports only Maya's iternal OpenGL based renderer.

Note that this method is called inside the rendering loop and as such, you should make this method as fast as possible - typically just returning a static/precalculated value.

Returns:
A reference to the render profile for this Shader. Your shader class should create this once (usually for the whole class) and return the same object each time this method is called.
Examples:
D3DViewportRenderer.cpp, and hlslShader.h.
MStatus populateRequirements ( const MPxHardwareShader::ShaderContext context,
MGeometryRequirements requirements 
) [virtual]

This method is not available in Python.

Override this method to let Maya know what surface data (e.g.

positions, normals, uv sets, tangents, binormals, etc) your shader needs in order to render the specified geometry. This is the only data your shader will be able to access when rendering the geometry. For example, if your shader needs uv set "foo" to render a texture, you must include that here before trying to use it in your render method.

The default implementation (in MPxHardwareShader) sets up the requirements for any public MVaryingParameters you have configured on this shader. If you override this method and use MVaryingParameters, you should make a call into the base class method as part of your implementation of the method.

Parameters:
[in]contextContext of the draw request (e.g. the surface being shaded, shading engine making the request).
[out]requirementsThe requirements structure you need to fill in.
Returns:
Status code
Status Codes:
MStatus getAvailableImages ( const MPxHardwareShader::ShaderContext context,
const MString uvSetName,
MStringArray imageNames 
) [virtual]

This method is not available in Python.

Maya will call this method to get your shader's list of images which are available for use in the UV texture editor for the UV set specified.

Typically, this list will include one entry for each texture using the specified UV set, however, your shader is free to return as many images as you wish (for example, blending between two textures, texture alpha masks, artificially shaded views of bump/normal maps, etc). Your shader's renderImage() method will be used to render the images themselves.

Parameters:
[in]contextContext of the draw request (e.g. the surface being shaded, shading engine making the request).
[in]uvSetNameName of a UV set the channel list should be filtered against.
[out]imageNamesArray in which to return the names of the images this shader defines which are valid for the uvSetName specified.
Returns:
Status code
Status Codes:
Examples:
hlslShader.h.
MStatus renderImage ( const MPxHardwareShader::ShaderContext context,
const MString imageName,
floatRegion  region,
int &  imageWidth,
int &  imageHeight 
) [virtual]

This method is not available in Python.

This method is obsolete.

Deprecated:
Use renderImage() that provides RenderParameters instead.

This method allows you to to render the background image used for this shader in the UV texture editor. The image requested will be one of the image names returned by your shader's getAvailableImages() method.

The implementation must return the dimensions of the image in the 'imageWidth' and 'imageHeight' parameters so that Maya can perform pixel snapping and other resolution-dependent operations.

The implementation can assume OpenGL context, model view projection matrix, and texture transformations have already been set. A default color of white will be set, however you are free to change this. The magnification filter will be set to either point or bilinear based on user configuration and should not be modified. The values of GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T are undefined on entry, and your implementation is responsible for setting them to appropriate values (e.g. GL_REPEAT).

The arguments contain the name of the image to render, and the vertex and texture coordinate values to use at each corner of the rectangular image being rendered. Your implementation is responsible for restoring the original the value of any OpenGL state that is modified.

For example:

        glPushAttrib( <GL bits your shader modifies> );
        glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT);

        ... setup material here ...

        glBegin( GL_QUADS);
        glTexCoord2f(region[0][0], region[0][1]);
        glVertex2f(region[0][0], region[0][1]);
        glTexCoord2f(region[0][0], region[1][1]);
        glVertex2f(region[0][0], region[1][1]);
        glTexCoord2f(region[1][0], region[1][1]);
        glVertex2f(region[1][0], region[1][1]);
        glTexCoord2f(region[1][0], region[0][1]);
        glVertex2f(region[1][0], region[0][1]);
        glEnd();

        glPopAttrib();
        glPopClientAttrib();
Parameters:
[in]contextContext of the draw request (e.g. the surface being shaded, shading engine making the request).
[in]imageNameName of the image to render. This corresponds to one of the image names returned by your shader's getAvailableImages() method.
[in]regionRectangular region to be rendered. The values of this parameter should be used to populate the vertex and texture coordinates of the rectangle being rendered.
[out]imageWidthNative width (e.g. texture pixel width) of the image. This should be for the entire image, not just the portion within 'region'.
[out]imageHeightNative height (e.g. texture pixel height) of the image. This should be for the entire image, not just the portion within 'region'.
Returns:
Status code
Status Codes:
Examples:
hlslShader.h.
MStatus renderImage ( const MPxHardwareShader::ShaderContext context,
const MString imageName,
floatRegion  region,
const MPxHardwareShader::RenderParameters parameters,
int &  imageWidth,
int &  imageHeight 
) [virtual]

This method is not available in Python.

This method allows you to to render the background image used for this shader in the UV texture editor.

The image requested will be one of the image names returned by your shader's getAvailableImages() method.

The implementation must return the dimensions of the image in the 'imageWidth' and 'imageHeight' parameters so that Maya can perform pixel snapping and other resolution-dependent operations.

The implementation can assume OpenGL context, model view projection matrix, and texture transformations have already been set. A default color of white will be set, however you are free to change this. The magnification filter will be set to either point or bilinear based on user configuration and should not be modified. The values of GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T are undefined on entry, and your implementation is responsible for setting them to appropriate values (e.g. GL_REPEAT).

The arguments contain the name of the image to render, and the vertex and texture coordinate values to use at each corner of the rectangular image being rendered. Your implementation is responsible for restoring the original the value of any OpenGL state that is modified.

For example:

        glPushAttrib( <GL bits your shader modifies> );
        glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT);

        ... setup material here ...

        glBegin( GL_QUADS);
        glTexCoord2f(region[0][0], region[0][1]);
        glVertex2f(region[0][0], region[0][1]);
        glTexCoord2f(region[0][0], region[1][1]);
        glVertex2f(region[0][0], region[1][1]);
        glTexCoord2f(region[1][0], region[1][1]);
        glVertex2f(region[1][0], region[1][1]);
        glTexCoord2f(region[1][0], region[0][1]);
        glVertex2f(region[1][0], region[0][1]);
        glEnd();

        glPopAttrib();
        glPopClientAttrib();
Parameters:
[in]contextContext of the draw request (e.g. the surface being shaded, shading engine making the request).
[in]imageNameName of the image to render. This corresponds to one of the image names returned by your shader's getAvailableImages() method.
[in]regionRectangular region to be rendered. The values of this parameter should be used to populate the vertex and texture coordinates of the rectangle being rendered.
[in]parametersAdditional parameters on how to render the image. The values reflect the image settings of the UV editor.
[out]imageWidthNative width (e.g. texture pixel width) of the image. This should be for the entire image, not just the portion within 'region'.
[out]imageHeightNative height (e.g. texture pixel height) of the image. This should be for the entire image, not just the portion within 'region'.
Returns:
Status code
Status Codes:
MStatus renderSwatchImage ( MImage image) [virtual]

If the shader specifies to override swatch rendering, then this method must be overridden in order to draw anything into a swatch.

The shader will only draw a swatch if it has been registered to do so, by providing a valid classification during MFnPlugin::registerNode(). The shader should provide a classification that defines a swatch rendering node such as : "shader/surface/utility/:drawdb/shader/surface/myCustomShader:swatch/myCustomShaderSwatchGenerator" and have "myCustomShaderSwatchGenerator" registered has a swatch renderer : MSwatchRenderRegister::registerSwatchRender("myCustomShaderSwatchGenerator", MHWShaderSwatchGenerator::createObj );

The default implementation is to draw nothing. The basic logic to draw a swatch is as follows:

  • Determine the size of the swatch required. This is the dimensions of the MImage passed in as an argument. The pixels for the MImage will have been pre-allocated. The format of the pixels is 32-bit R,G,B,A, with 8-bits per channel.
  • Either use an offscreen "swatch context" provided to you or use your own offscreen context. The provided context is available via MHardwareRenderer::makeSwatchContextCurrent(). Note that the swatch context may be smaller than the desired image size. In this case the rendering dimensions will be clamped.
  • Either use swatch geometry provided to you, or use your own swatch geometry. The provided geometry is available via MHardwareRenderer::referenceDefaultGeometry(). The possible "default" geometries are either a sphere, cube or plane.
  • Either use the provided default light and camera or set up your own. MHardwareRenderer provides methods to retrieve the defaults.
  • Read back the swatch context into the provided MImage. The convenience method MHardwareRenderer::readSwatchContextPixels() can be used. By default the format of the MImage and the swatch context are the same, so the user does not need to worry about this. The context will read into the pre-allocated MImage pixels.
  • Unreference any swatch geometry used for rendering using MHardwareRenderer::dereferenceGeometry().
Parameters:
[in,out]imageImage object to which this method must write the rendered swatch. On input the image's dimensions are already set and pixel storage already allocated.
Returns:
Status code
Status Codes:
Examples:
hlslShader.h.
MPxHardwareShader * getHardwareShaderPtr ( MObject object) [static]

This is a static convenience method to be able to get an MPxHardwareShader from an MObject provided by a swatch generator class (Class derived from MSwatchRenderRegister).

Parameters:
[in]objectThe object to examine.
Returns:
A pointer to an MPxHardwareShader. If the method failed for any reason then a 0 (null) will be returned.
Examples:
D3DViewportRenderer.cpp.
MString findResource ( const MString name,
const MString shaderPath,
MStatus status = NULL 
) [static]

This is a static utility to find the full path to a shader resource (typically a texture).

This method will search the list of paths in the MAYA_HW_SHADER_RESOURCE_PATH environment variable, resolving relative paths based on the directory containing the shader.

Parameters:
[in]nameThe name of the resource to look for (e.g. "normals.dds")
[in]shaderPathThe full path to the current shader (e.g. "C:/shaders/myshader.fx")
[in]statusIf a pointer to a status is provided, the status will be set to MS::kSuccess if the resource was found, and MS::kFailure otherwise.
Returns:
The full path of the resource (e.g. "C:/shaders/textures/normals.dds")
const char * className ( ) [static]

Returns the name of this class.

Returns:
The name of this class.

Reimplemented from MPxNode.


MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader
MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader MPxHardwareShader