Click or drag to resize
DigitalRuneRender Context
What is the render context?

The class RenderContext is automatically created by the graphics service. It is a collection of data that needs to be passed to every Render method. The render context should contain all information required to render an object or to perform a rendering step. (That means, if a renderer requires additional information, it should be able to find the information through the render context.)

In most cases the active camera (property CameraNode), the render pass (property RenderPass) and the current scene (property Scene) need to be set for rendering. Additional information in the render context might be required depending on the effect bindings or renderer that are used.

Additional information can be stored in the RenderContextData dictionary.

How to Use the render context

Several properties of the render context are set automatically by the graphics service. These properties include: DeltaTime, GraphicsService, PresentationTarget, RenderTarget, Screen, SourceTexture, Viewport

Other properties are automatically set by scene node renderers. These properties include: MaterialBinding, MaterialInstanceBinding, PassIndex, SceneNode

Following properties should be managed explicitly in your custom graphics screens or your custom renderers: CameraNode, Data, GBuffer0, GBuffer1, GBuffer2, GBuffer3, LightBuffer0, LightBuffer1, RenderPass, RenderTarget, Scene, SourceTexture, Technique, Viewport

Here are some guidelines for using the render context:

  • If you have a piece of information and there is a render context property for it, then update the render context. For example, if a graphics screen has a Scene then set the RenderContextScene property at the beginning of the graphics screen's Render method. Especially before calling a renderer, make sure that the render context is up-to-date.

  • All graphics screens and renderers should restore the original render context at the end of their Render method.

  • It is allowed that renderers exchange the current render targets with another render target of the same format. This can be necessary for performance optimizations. After a renderer has executed, be prepared that it might have changed the current RenderTarget, GBuffer0 or other render targets. Therefore, it is not recommended to store the current render target or G-buffer in a local variable. Use the render context properties.

Here are some common usage examples:

Example (C#): A Simple Graphics Screen Render Method
protected override void OnRender(RenderContext context)
{
  // Update render context.
  context.CameraNode = _myCameraNode;
  context.Scene = _myScene;

  // Render...

  // Clean up.
  context.Scene = null;
  context.CameraNode = null;
}
Example (C#): Calling a MeshRenderer
context.RenderPass = "Default";
_renderer.Render(query.SceneNodes, context);
context.RenderPass = null;
Example (C#): Calling a Post-Processor
context.SourceTexture = sourceTexture;
context.RenderTarget = renderTarget;
context.Viewport = new Viewport(0, 0, renderTarget.Width, renderTarget.Height);
postProcessor.Process(context);
context.SourceTexture = null;
Example (C#): Switching a Render Target
graphicsDevice.SetRenderTarget(myRenderTarget);
context.RenderTarget = myRenderTarget;
context.Viewport = graphicsDevice.Viewport;
Custom render contexts

It is possible to use a custom render context:

  1. Create a new class that derives from RenderContext and add the desired members (properties, methods, etc.).

    C#
    public class MyRenderContext : RenderContext
    {
      // TODO: Add members here.
    }
  2. Create a new class that derives from GraphicsManager. and override the method CreateRenderContext.

    C#
    public class MyGraphicsManager : GraphicsManager
    {
      protected override RenderContext CreateRenderContext()
      {
        return new MyRenderContext();
      }
    }
  3. In the application use the new class instead of the default GraphicsManager.