Skip to content

Guide: Using render buffers

Alan Stagner edited this page Jun 10, 2020 · 1 revision

Render buffers in Beef let you render into arbitrary surfaces and then use them as textures. There are two types of render buffer: ColorRenderBuffer and DepthStencilRenderBuffer.

Creating a ColorRenderBuffer

To create a ColorRenderBuffer, you first need a texture to render into. This can either be a Texture2D, or a TextureCube, and it must have been constructed with isRenderTarget = true

let myTex = new Texture2D(graphicsDevice, 1024, 1024, .Color, 1, true); // isRenderTarget must be true
let myColorBuffer = new ColorRenderBuffer(graphicsDevice, myTex, 1); // you can optionally enable multisampling via the multisampleCount parameter. 1 disables multisampling.

let myCubeTex = new TextureCube(graphicsDevice, 1024, .Color, true);
let myColorBuffer2 = new ColorRenderBuffer(graphicsDevice, myCubeTex, .PositiveX); // note: when rendering into cubemaps, you much specify which cube face you are rendering to

Creating a DepthStencilRenderBuffer

A DepthStencilRenderBuffer does not need a texture to be created:

let myDepthBuffer = new DepthStencilRenderBuffer(graphicsDevice, .D24S8, 1024, 1024);

Rendering to color & depth buffers

To render to a color & depth buffer pair, pass them into SetRenderTarget or SetRenderTargets:

GraphicsDevice.SetRenderTarget( myColorBuffer, myDepthBuffer );
GraphicsDevice.SetRenderTargets( scope ColorRenderBuffer[] { myColorBuffer, myColorBuffer2 }, myDepthBuffer ); // you can also pass an array of ColorRenderBuffers to render to, which a pixel shader that supports MRT can render to via the COLOR0 .. COLORn semantics

If you only want to temporarily change the render target and then set it back when you're done, you can wrap your rendering code with PushCurrentRenderTarget and PopCurrentRenderTarget:

GraphicsDevice.PushCurrentRenderTarget();

// your render code here

GraphicsDevice.PopCurrentRenderTarget();

Cleaning up

If you created your texture with mipmaps enabled, or MSAA enabled, you'll need to resolve the render buffer after you have rendered into it:

GraphicsDevice.ResolveTarget( myColorBuffer ); // this will generate mip levels & resolve MSAA

And now you can use the texture you used to create the render buffer like any other, passing it into Effects via SetTexture:

myEffect.SetTexture( myTex );
myEffect.ApplyEffect( 0 );