Textures and render textures

Xenko uses the class to interact with texture objects in code.

For more information about rendering to a texture, see Render textures.

To load a texture from an asset in Xenko, call this function:

This automatically generates a texture object with all its fields correctly filled.

Create a texture

You can also create textures without any assets (eg to be used as render target). To do this, call the constructor of the class. See the Texture class reference to get the full list of available options and parameters. Some texture formats might not be available on all platforms.

Create a render target

Code: Create a custom render target and depth buffer

Note

Don't forget the flag to enable the render target behavior.

Make sure the PixelFormat is correct, especially for the depth buffer. Be careful of the available formats on the target platform.

Once these buffers are created, you can can easily set them as current render textures.

Code: Use a render target

  1. // settings the render textures
  2. CommandList.SetRenderTargetAndViewport(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer);
Note

Make sure both the render target and the depth buffer have the same size. Otherwise, the depth buffer isn't used.

You can set multiple render textures at the same time. See the overloads of SetRenderTargets(Texture, Texture[]) and method.

Note

Clear a render target

To clear render textures, call the Clear(Texture, Color4) and methods.

Note

Don't forget to clear the BackBuffer and the each frame. If you don't, you might get unexpected behavior depending on the device. If you want to keep the contents of a frame, use an intermediate render target.

Viewport

SetRenderTargetAndViewport(Texture, Texture) adjusts the current to the full size of the render target.

If you only want to render to a subset of the texture, set the render target and viewport separately using SetRenderTarget(Texture, Texture) and .

You can bind multiple viewports using SetViewports(Viewport[]) and overloads for use with a geometry shader.

Code: Set the viewports

  1. // example of a full HD buffer
  2. CommandList.SetRenderTarget(GraphicsDevice.Presenter.DepthStencilBuffer, GraphicsDevice.Presenter.BackBuffer); // no viewport set
  3. // example of setting the viewport to have a 10-pixel border around the image in a full HD buffer (1920x1080)
  4. var viewport = new Viewport(10, 10, 1900, 1060);
  5. CommandList.SetViewport(viewport);

Code: Set the scissor

See also