Textures and render textures

AdvancedProgrammer Texture class to interact with texture objects in code. Render textures.

Load a texture

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

  1. // loads the texture called duck.dds (or .png etc.)var myTexture = Content.Load<Texture>("duck");

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

Create a texture

Texture 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.

Code: Create a texture

  1. var myTexture = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource);

Render textures

Create a render target

GraphicsPresenter class always provides a default render target and a depth buffer. They are accessible through the BackBuffer and DepthStencilBuffer properties. The presenter is exposed by the Presenter property of the GraphicsDevice. However, you might want to use your own buffer to perform off-screen rendering or post-processes. As a result, Xenko offers a simple way to create textures that can act as render textures and a depth buffers.

Code: Create a custom render target and depth buffer

  1. // render targetvar myRenderTarget = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget);// writable depth buffervar myDepthBuffer = Texture.New2D(GraphicsDevice, 512, 512, false, PixelFormat.D16_UNorm, TextureFlags.DepthStencil);
Note

RenderTarget 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.

Use a render target

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

Code: Use a render target

  1. // settings the render texturesCommandList.SetRenderTargetAndViewport(myDepthBuffer, myRenderTarget);// setting the default render targetCommandList.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. SetRenderTargets(Texture, Texture[]) and SetRenderTargetsAndViewport(Texture, Texture[]) method.

Note

BackBuffer is displayed on screen, so you need to render it to display something.

Clear a render target

Clear(Texture, Color4) and Clear(Texture, DepthStencilClearOptions, Single, Byte) methods.

Code: Clear the targets

  1. CommandList.Clear(GraphicsDevice.Presenter.BackBuffer, Color.Black);CommandList.Clear(GraphicsDevice.Presenter.DepthStencilBuffer, DepthStencilClearOptions.DepthBuffer); // only clear the depth buffer
Note

BackBuffer and the DepthStencilBuffer 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 Viewport to the full size of the render target. SetRenderTarget(Texture, Texture) and SetViewport(Viewport). SetViewports(Viewport[]) and SetViewports(Int32, Viewport[]) overloads for use with a geometry shader.

Code: Set the viewports

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

Scissor

SetScissorRectangle(Rectangle) and SetScissorRectangles(Rectangle[]) methods set the scissors. Unlike the viewport, you need to provide the coordinates of the location of the vertices defining the scissor instead of its size.

Code: Set the scissor

  1. // example of setting the scissor to crop the image by 10 pixel around it in a full hd buffer (1920x1080)var rectangle = new Rectangle(10, 10, 1910, 1070);CommandList.SetScissorRectangles(rectangle);var rectangles = new[] { new Rectangle(10, 10, 1900, 1060), new Rectangle(0, 0, 256, 256) };CommandList.SetScissorRectangles(rectangles);

See also

  • Render textures