# graphics3d/rendertarget (vanilla.js 0.4.0)

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

Render target. (offscreen framebuffer + color texture [+ depth renderbuffer / depth texture])

## RenderTarget extends Object

Render target. (offscreen framebuffer + color texture [+ depth renderbuffer / depth texture])
- The basic unit of the post processing chain.
- Multiple color attachments (MRT) / half precision float color (HDR) / depth texture / multisample / depth bits can be enabled as options.
  { colorAttachmentCount: 1, useFloatColor: false, useDepthTexture: false, sampleCount: 0, depthBits: 16 }
- If sampleCount is 2 or more, draws into a multisample (MSAA) renderbuffer and resolve() resolves it into the color texture (and the depth texture).
  If it exceeds what the device accepts (MAX_SAMPLES, the sample counts each format accepts), it is reduced to the largest number below that, and if none is accepted or the framebuffer is incomplete, it is built without multisampling. (getSampleCount)
  When multisampled, getFramebuffer is the multisample framebuffer that is drawn into, and getResolveFramebuffer is the framebuffer with the textures attached. Call resolve() before reading the textures.
- resolve() leaves the multisample color and depth as they are after resolving. (Discarding them with invalidateFramebuffer is not done because it 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)
- depthBits is the bit count (16 or 24) of the depth renderbuffer. The depth texture is always 24 bits. When multisampled with a depth texture, the multisample depth is also built at 24 bits and resolved as is.

Example: Draw to a multisample render target and resolve it to textures

```js
import { RenderTarget } from "./build/vanilla.js";

const canvas = document.createElement("canvas");
const webGL2RenderingContext = canvas.getContext("webgl2");
const renderTarget = new RenderTarget(webGL2RenderingContext, 256, 256, true, { useDepthTexture: true, sampleCount: 4 });
renderTarget.bind();
webGL2RenderingContext.clearColor(0.2, 0.4, 0.8, 1.0);
webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);
renderTarget.resolve();
webGL2RenderingContext.bindFramebuffer(webGL2RenderingContext.FRAMEBUFFER, null);
const sampleCount = renderTarget.getSampleCount();
const colorTexture = renderTarget.getColorTexture();
const depthTexture = renderTarget.getDepthTexture();
console.log(sampleCount, colorTexture !== null, depthTexture !== null);
renderTarget.destroy();
```

- `constructor(webGL2RenderingContext, width, height, useDepth, options = null)` (constructor, since 0.3.0): Constructor.
  - `webGL2RenderingContext` WebGL2RenderingContext: WebGL2 rendering context.
  - `width` number: Width. (pixels)
  - `height` number: Height. (pixels)
  - `useDepth` boolean: Whether to attach depth.
  - `options` object | null: { colorAttachmentCount = 1, useFloatColor = false, useDepthTexture = false, sampleCount = 0, depthBits = 16 } (all defaults when null)

  Example: Two color attachments (MRT) and float color

  ```js
  import { RenderTarget } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const renderTarget = new RenderTarget(webGL2RenderingContext, 320, 180, true, { colorAttachmentCount: 2, useFloatColor: true });
  const firstTexture = renderTarget.getColorTexture(0);
  const secondTexture = renderTarget.getColorTexture(1);
  console.log(firstTexture !== null, secondTexture !== null);
  renderTarget.resize(640, 360);
  const width = renderTarget.getWidth();
  const height = renderTarget.getHeight();
  console.log(width, height);
  renderTarget.destroy();
  ```

- `resize(width, height)` (method, since 0.3.0): Resize. (Ignored if the same size, otherwise recreates resources)
  - `width` number: New width. (pixels)
  - `height` number: New height. (pixels)
- `readSupportedSampleCount(requestedSampleCount, internalFormatList)` (method, 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)
  - `requestedSampleCount` number: Requested sample count.
  - `internalFormatList` Array<number>: List of renderbuffer internal formats. (reduced to a count every format accepts)
  - returns number: Sample count the device accepts. (0 when below 2)
- `resolve(isAttachmentKept = false)` (method, since 0.4.0): Resolves multisample into textures. (For each color attachment and the depth texture, restores the previously bound framebuffer)
  - The multisample color and depth stay as they are after resolving. (They are not discarded with invalidateFramebuffer, which added several to tens of ms per frame in Safari on iPad and iPhone and showed no measured gain)
  - `isAttachmentKept` boolean: No longer used. (Kept to match the old shape, either way the multisample color and depth are not discarded after resolving)

  Example: Keep drawing into the same target after resolving

  ```js
  import { RenderTarget } from "./build/vanilla.js";
  
  const canvas = document.createElement("canvas");
  const webGL2RenderingContext = canvas.getContext("webgl2");
  const renderTarget = new RenderTarget(webGL2RenderingContext, 128, 128, true, { sampleCount: 4 });
  renderTarget.bind();
  webGL2RenderingContext.clearColor(0.1, 0.2, 0.3, 1.0);
  webGL2RenderingContext.clear(webGL2RenderingContext.COLOR_BUFFER_BIT | webGL2RenderingContext.DEPTH_BUFFER_BIT);
  renderTarget.resolve(true);
  renderTarget.bind();
  renderTarget.resolve();
  webGL2RenderingContext.bindFramebuffer(webGL2RenderingContext.FRAMEBUFFER, null);
  console.log(renderTarget.getSampleCount());
  ```

- `bind()` (method, since 0.3.0): Binds as the rendering target. (framebuffer + viewport)
- `destroy()` (method, since 0.3.0): Destroy. (Releases GL resources)
- `destroyMultisample()` (method, since 0.4.0): Deletes the multisample framebuffer and renderbuffers.
- `getWebGL2RenderingContext()` (method, since 0.3.0): Returns the rendering context.
  - returns WebGL2RenderingContext: Rendering context.
- `getFramebuffer()` (method, since 0.3.0): Returns the framebuffer. (The framebuffer that is drawn into, the multisample framebuffer when multisampled)
  - returns WebGLFramebuffer | null: Framebuffer to draw into. (the multisample framebuffer when multisampled, null after destroy)
- `getResolveFramebuffer()` (method, since 0.4.0): Returns the framebuffer with the textures attached. (Same as getFramebuffer if not multisampled)
  - returns WebGLFramebuffer | null: Framebuffer with the color textures attached. (null after destroy)
- `getSampleCount()` (method, since 0.4.0): Returns the sample count actually built. (0 means no multisampling)
  - returns number: Sample count actually built. (0 means no multisampling)
- `getDepthBits()` (method, since 0.4.0): Returns the depth renderbuffer bit count. (16 or 24)
  - returns number: Depth renderbuffer bit count. (16 or 24)
- `getColorTexture(attachmentIndex = 0)` (method, since 0.3.0): Returns the color texture. (attachment index, default 0)
  - `attachmentIndex` number: Color attachment index. (default 0)
  - returns WebGLTexture | null: Color texture. (null when the index is out of range)
- `getDepthTexture()` (method, since 0.3.0): Returns the depth texture. (Exists only with the useDepthTexture option)
  - returns WebGLTexture | null: Depth texture. (only when both useDepth and useDepthTexture are on, otherwise null)
- `getColorAttachmentCount()` (method, since 0.3.0): Returns the color attachment count.
  - returns number: Number of color attachments.
- `getUseFloatColor()` (method, since 0.3.0): Returns whether float color is used.
  - returns boolean: True when the color is RGBA16F.
- `getUseDepthTexture()` (method, since 0.3.0): Returns whether the depth texture is used.
  - returns boolean: True when a depth texture is used.
- `getWidth()` (method, since 0.3.0): Returns the width.
  - returns number: Width. (pixels, 0 after destroy)
- `getHeight()` (method, since 0.3.0): Returns the height.
  - returns number: Height. (pixels, 0 after destroy)
