next up previous contents
Next: 3.24 Functions for Shaders Up: 3. Using and Writing Previous: 3.22 Lightmap Shaders.x

3.23 Render Pass Merge Shaders3.1

   Multipass rendering allows rendering a scene in multiple passes, each containing a subset of the full scene, and then putting the results together into the final image. This step is called merging, and allws specifying a renderpass merge shader that makes decisions about how samples at the same raster coordinate should be combined. See the section on Multipass Rendering on page [*] for a detailed explanation.

Here is an example of a simple renderpass merge shader that could be used in the scene example on page [*]:

    DLLEXPORT int mymerge_version(void) {return(1);}

    DLLEXPORT miBoolean mymerge(
        void            *result,
        miState         *state,
        void            *param)
    {
        int              pass, npasses = state->options->npasses;
        int              closestpass = -1;
        miScalar         z, zbest = 1e10;
        void             *data;

        for (pass=0; pass < npasses; pass++)
            if (data = mi_renderpass_access(state, pass, miRC_IMAGE_Z)) {
                z = *(miScalar *)data;
                if (z > 0 && z < zbest) {       /* z == 0 means infinite */                         closestpass = pass;
                    zbest = z;
                }
            }
        if (closestpass >= 0)
            *(miColor *)mi_renderpass_access(state, -1, miRC_IMAGE_RGBA) = 
                *(miColor *)mi_renderpass_access(state, closestpass,
                                                 miRC_IMAGE_RGBA);  
        return(miTRUE);
    }

Multipass merge shaders have similar state variables and shader interface functionality available as  output shaders, plus the central function of merging,  mi_renderpass_access. This function allows access to all samples at the current raster coordinate to be merged. The shader can not access samples at other raster coordinates; merging operates strictly on a sample-by-sample basis.

The  mi_renderpass_access function returns a pointer to the specified frame buffer of the specified pass number. If this frame buffer does not exist or the pass number is out of range, a null pointer is returned. The result of the merge is stored into the target sample, a pointer to which can be obtained by passing pass number -1 to  mi_renderpass_access. Note that if the current pass involves both rendering and merging, one of the pass samples may be identical to the target sample, so that the pointer returned by  mi_renderpass_access for pass -1 is identical to the pointer returned by a pass number between 0 and the number of passes minus 1.

The example shader simply loops over all frame buffers and remembers the index of the closest one with the lowest positive depth value, ignoring null values (meaning infinite, or no sample). If renderpass mode is enabled, mental ray will automatically enable the depth frame buffer. After looping, the color is copied. If  output shaders exist that rely on other frame buffers, these should be copied as well. The shader can determine which frame buffers exist by checking state - > options - > write_image[] for nonzero values, using the same miRC_IMAGE_* frame buffer numbers that are also used as arguments for  mi_renderpass_access.


next up previous contents
Next: 3.24 Functions for Shaders Up: 3. Using and Writing Previous: 3.22 Lightmap Shaders.x
Copyright 2002 by mental images