# graphics3d/rendertarget (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/graphics3d/rendertarget/

Render target. (the original graphics3d/rendertarget.js)
- Offscreen framebuffer + color texture [+ depth renderbuffer / depth texture]. The basic unit of a post-processing chain.
- Options can enable multiple color attachments (MRT) / half-float color (HDR) / depth texture / multisampling / depth bits.
  The original options object { colorAttachmentCount, useFloatColor, useDepthTexture, sampleCount, depthBits } became RenderTargetOptions.
- When sample_count is 2 or more, drawing goes to multisample (MSAA) renderbuffers and resolve() resolves them into the color textures (and the depth texture).
  If it exceeds what the device supports (MAX_SAMPLES, and on the web also the sample counts supported per format), it is reduced to the largest supported value below it, and if that is unsupported or the framebuffer is incomplete, it is built without multisampling. (get_sample_count)
  Desktop GL 3.3 has no function to query the supported sample counts per format (glGetInternalformativ, GL 4.2), so only MAX_SAMPLES is checked. (3.3 supports up to MAX_SAMPLES for float and non-integer color formats)
  With multisampling, get_framebuffer is the multisample framebuffer drawn to, and get_resolve_framebuffer is the framebuffer with the textures attached. Call resolve() before reading the textures.
- depth_bits is the bit count of the depth renderbuffer (16 or 24). The depth texture is always 24-bit. With multisampling and a depth texture, the multisample depth is also built as 24-bit and resolved as is.
- The original enables EXT_color_buffer_float to render RGBA16F. Desktop GL 3.3 supports it by default, and on the web glow enables it when
  creating the context.
- When a GL object cannot be created, the original continues as is, but the Rust version returns an error (Result).

## RenderTargetOptions (struct) (Clone, Copy, Debug, PartialEq, Eq, Default)

Render target options. (the original options object, the defaults are 1 color / RGBA8 / 16-bit depth renderbuffer / no multisampling)

Example: Default, 4 MRT, HDR and depth texture options

```rust
use vanilla::RenderTargetOptions;

let default_options = RenderTargetOptions::default();
assert_eq!(default_options.color_attachment_count, 1);
assert!(!default_options.use_float_color);
assert_eq!(default_options.sample_count, 0);
assert_eq!(default_options.depth_bits, 16);

let geometry_options = RenderTargetOptions { color_attachment_count: 4, use_float_color: true, use_depth_texture: true, ..RenderTargetOptions::default() };
assert_eq!(geometry_options.depth_bits, 16);
```

- `pub color_attachment_count: usize` (field, since 0.3.2): Color attachment count. (MRT, built as 1 when 0, default 1)
  - returns usize
- `pub use_float_color: bool` (field, since 0.3.2): Whether to build color as half float (RGBA16F, HDR). (RGBA8 when false, default false)
  - returns bool
- `pub use_depth_texture: bool` (field, since 0.3.2): Whether to build depth as a samplable texture (24-bit). (a depth renderbuffer when false, used only when use_depth is true, default false)
  - returns bool
- `pub sample_count: i32` (field, since 0.4.0): Multisample count. (no multisampling below 2)
  - returns i32
- `pub depth_bits: i32` (field, since 0.4.0): Depth renderbuffer bit count. (24 for 24, 16 otherwise)
  - returns i32

## RenderTarget (struct)

Render target.

Example: Draw the scene to an HDR render target and read the color texture in the next pass

```rust
use vanilla::{RenderTarget, RenderTargetOptions};

fn create_scene_target(gl: &vanilla::GlContext) -> Result<RenderTarget, String> {
	let options = RenderTargetOptions { use_float_color: true, ..RenderTargetOptions::default() };
	RenderTarget::new(gl, 2, 2, true, Some(options))
}

fn render_scene(gl: &vanilla::GlContext, scene_render_target: &mut RenderTarget, width: i32, height: i32) -> Result<(), String> {
	scene_render_target.resize(gl, width, height)?;
	scene_render_target.bind(gl);
	unsafe {
		gl.clear_color(0.0, 0.0, 0.0, 1.0);
		gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT);
	}
	let scene_color_texture = scene_render_target.get_color_texture(0);
	unsafe {
		gl.bind_framebuffer(glow::FRAMEBUFFER, None);
		gl.bind_texture(glow::TEXTURE_2D, scene_color_texture);
	}
	Ok(())
}
```

- `pub fn read_supported_sample_count(gl: &GlContext, requested_sample_count: i32, internal_format_list: &[u32]) -> i32` (static, since 0.4.0): The sample count the device accepts. (The requested count reduced to the largest number below MAX_SAMPLES and the sample counts each format accepts, 0 if below 2)
  - `gl` &GlContext: GL context.
  - `requested_sample_count` i32: Desired sample count. (no multisampling below 2)
  - `internal_format_list` &[u32]: List of renderbuffer formats to be used together. (such as glow::RGBA8, glow::DEPTH_COMPONENT24, not checked on desktop GL 3.3)
  - returns i32: Sample count that can be built. (2 or more) 0 if the request is below 2 or the supported count is below 2.

  Example: Check ahead whether 4x MSAA is supported

  ```rust
  use vanilla::RenderTarget;
  
  fn read_msaa_sample_count(gl: &vanilla::GlContext) -> i32 {
  	RenderTarget::read_supported_sample_count(gl, 4, &[glow::RGBA8, glow::DEPTH_COMPONENT16])
  }
  ```

- `pub fn new(gl: &GlContext, width: i32, height: i32, use_depth: bool, options: Option<RenderTargetOptions>) -> Result<RenderTarget, String>` (constructor, since 0.3.2): Constructor. (default options when options is None)
  - At a size of 0 x 0, no GL objects are built. (resize skips it because the size is the same)
  - `gl` &GlContext: GL context. (taken out with Graphic::get_gl)
  - `width` i32: Width. (pixels)
  - `height` i32: Height. (pixels)
  - `use_depth` bool: Whether to attach a depth attachment. (a renderbuffer, or a depth texture with use_depth_texture)
  - `options` Option<RenderTargetOptions>: Options. (RenderTargetOptions::default() when None)
  - returns Result<RenderTarget, String>: New render target. Err(GL error message) if a framebuffer, texture or renderbuffer cannot be created.

  Example: Draw with 4x MSAA and resolve into the texture

  ```rust
  use vanilla::{RenderTarget, RenderTargetOptions};
  
  fn render_antialiased(gl: &vanilla::GlContext) -> Result<(), String> {
  	let options = RenderTargetOptions { sample_count: 4, depth_bits: 24, ..RenderTargetOptions::default() };
  	let render_target = RenderTarget::new(gl, 1280, 720, true, Some(options))?;
  	render_target.bind(gl);
  	render_target.resolve(gl);
  	let color_texture = render_target.get_color_texture(0);
  	assert!(color_texture.is_some());
  	Ok(())
  }
  ```

- `pub fn resize(&mut self, gl: &GlContext, width: i32, height: i32) -> Result<(), String>` (method, since 0.3.2): Resizes. (ignored for the same size, otherwise the resources are rebuilt)
  - Rebuilding deletes the previous textures, so get them again with get_color_texture. The multisample count is also chosen again.
  - `gl` &GlContext: GL context.
  - `width` i32: New width. (pixels)
  - `height` i32: New height. (pixels)
  - returns Result<(), String>: Ok if rebuilt or the same size. Err (the GL error text, what was built so far is deleted and the size is 0) if a GL object cannot be made.
- `pub fn resolve(&self, gl: &GlContext)` (method, since 0.4.0): Resolves multisample into textures. (each color attachment and the depth texture, returns to the previously bound framebuffer, original resolve())
  - The multisample color and depth stay as they are after resolving. (They are not discarded with invalidate_framebuffer, which added several to tens of ms per frame in Safari on iPad and iPhone and showed no measured gain; drawing more after resolving also works)
  - `gl` &GlContext: GL context.
- `pub fn resolve_with_attachment_kept(&self, gl: &GlContext, is_attachment_kept: bool)` (method, since 0.4.0): Resolves multisample into textures. (Kept to match the old shape, original resolve(isAttachmentKept))
  - `gl` &GlContext: GL context.
  - `is_attachment_kept` bool: No longer used. (Either way the multisample color and depth are not discarded after resolving)

  Example: Keep drawing after resolving

  ```rust
  use vanilla::{RenderTarget, RenderTargetOptions};
  
  fn render_twice(gl: &vanilla::GlContext) -> Result<(), String> {
  	let options = RenderTargetOptions { sample_count: 4, ..RenderTargetOptions::default() };
  	let render_target = RenderTarget::new(gl, 128, 128, true, Some(options))?;
  	render_target.bind(gl);
  	render_target.resolve_with_attachment_kept(gl, true);
  	render_target.bind(gl);
  	render_target.resolve(gl);
  	println!("{}", render_target.get_sample_count());
  	Ok(())
  }
  ```

- `pub fn bind(&self, gl: &GlContext)` (method, since 0.3.2): Binds as the rendering target. (framebuffer + viewport, the multisample framebuffer when multisampled)
  - `gl` &GlContext: GL context.
- `pub fn destroy(&mut self, gl: &GlContext)` (method, since 0.3.2): Destroy. (releases GL resources)
  - The size goes back to 0, so it can be rebuilt with resize afterward.
  - `gl` &GlContext: GL context used at creation.
- `pub fn get_framebuffer(&self) -> Option<GlFramebuffer>` (method, since 0.3.2): Returns the framebuffer. (The framebuffer that is drawn into, the multisample framebuffer when multisampled)
  - returns Option<GlFramebuffer>: Framebuffer drawn to. None after destroy or if not built.
- `pub fn get_resolve_framebuffer(&self) -> Option<GlFramebuffer>` (method, since 0.4.0): Returns the framebuffer with the textures attached. (same as get_framebuffer if not multisampled)
  - returns Option<GlFramebuffer>: Framebuffer with the color textures attached. None after destroy or if not built.
- `pub fn get_sample_count(&self) -> i32` (method, since 0.4.0): Returns the sample count actually built. (0 means no multisampling)
  - returns i32: Multisample count. (0 means no multisampling, may be smaller than requested)
- `pub fn get_depth_bits(&self) -> i32` (method, since 0.4.0): Returns the depth renderbuffer bit count. (16 or 24)
  - returns i32: Depth renderbuffer bit count. (16 or 24)
- `pub fn get_color_texture(&self, attachment_index: usize) -> Option<GlTexture>` (method, since 0.3.2): Returns the color texture. (attachment index, the original default is 0)
  - `attachment_index` usize: Color attachment index. (from 0)
  - returns Option<GlTexture>: Color texture of that attachment. None if the index is at or above the attachment count or it is not built.
- `pub fn get_depth_texture(&self) -> Option<GlTexture>` (method, since 0.3.2): Returns the depth texture. (exists only with the use_depth_texture option)
  - returns Option<GlTexture>: Depth texture. None unless both use_depth and use_depth_texture are true.
- `pub fn get_color_attachment_count(&self) -> usize` (method, since 0.3.2): Returns the color attachment count.
  - returns usize: Color attachment count. (1 or more)
- `pub fn get_use_float_color(&self) -> bool` (method, since 0.3.2): Returns whether float color is used.
  - returns bool: true if the color is RGBA16F, false if RGBA8.
- `pub fn get_use_depth_texture(&self) -> bool` (method, since 0.3.2): Returns whether the depth texture is used.
  - returns bool: The use_depth_texture option.
- `pub fn get_width(&self) -> i32` (method, since 0.3.2): Returns the width.
  - returns i32: Width. (pixels, 0 after destroy)
- `pub fn get_height(&self) -> i32` (method, since 0.3.2): Returns the height.
  - returns i32: Height. (pixels, 0 after destroy)
