vanilladocs
vanillapunch.com
API / graphics3d/rendertarget

graphics3d/rendertarget

렌더 타겟. (원본 graphics3d/rendertarget.js)

  • 오프스크린 프레임버퍼 + 컬러 텍스처 [+ 깊이 렌더버퍼 / 깊이 텍스처]. 포스트 프로세싱 체인의 기본 단위다.
  • 옵션으로 다중 컬러 어태치먼트(MRT) / 반정밀 실수 컬러(HDR) / 깊이 텍스처 / 멀티샘플 / 깊이 비트를 켤 수 있다. 원본 옵션 객체 { colorAttachmentCount, useFloatColor, useDepthTexture, sampleCount, depthBits } 는 RenderTargetOptions 로 옮겼다.
  • sample_count 가 2 이상이면 멀티샘플(MSAA) 렌더버퍼에 그리고 resolve() 가 컬러 텍스처(와 깊이 텍스처)로 푼다. 기기가 받는 수(MAX_SAMPLES, 웹은 포맷마다 받는 표본 수도)를 넘으면 그 밑의 가장 큰 수로 줄이고, 못 받거나 프레임버퍼가 완성되지 않으면 멀티샘플 없이 짓는다. (get_sample_count) 데스크탑 GL 3.3 에는 포맷마다 받는 표본 수를 묻는 함수(glGetInternalformativ, GL 4.2)가 없어 MAX_SAMPLES 만 본다. (3.3 은 실수, 정수가 아닌 색 포맷이면 MAX_SAMPLES 까지 받는다) 멀티샘플이면 get_framebuffer 는 그리는 멀티샘플 프레임버퍼, get_resolve_framebuffer 는 텍스처가 붙은 프레임버퍼다. 텍스처를 읽기 전에 resolve() 를 부른다.
  • depth_bits 는 깊이 렌더버퍼의 비트 수(16 이나 24)다. 깊이 텍스처는 늘 24 비트다. 멀티샘플이면서 깊이 텍스처를 쓰면 멀티샘플 깊이도 24 비트로 지어 그대로 푼다.
  • 원본은 RGBA16F 를 그리려고 EXT_color_buffer_float 를 켠다. 데스크탑 GL 3.3 은 기본으로 되고, 웹은 glow 가 컨텍스트를 만들 때 켜 둔다.
  • GL 객체를 못 만들면 원본은 그대로 진행하지만, 러스트 판은 오류(Result)로 돌려준다.

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

RenderTargetOptionsstruct traits Clone, Copy, Debug, PartialEq, Eq, Default

소스source

렌더 타겟 옵션. (원본 options 객체, 기본값은 컬러 1장 / RGBA8 / 깊이 렌더버퍼 16 비트 / 멀티샘플 없음)

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

예제Example기본값과 MRT 4장, HDR, 깊이 텍스처 옵션Default, 4 MRT, HDR and depth texture options
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);
필드fieldpub color_attachment_count: usize#0.3.2 부터since 0.3.2소스source

컬러 어태치먼트 수. (MRT, 0 이면 1 로 짓는다, 기본 1)

Color attachment count. (MRT, built as 1 when 0, default 1)

반환Returnsusize

필드fieldpub use_float_color: bool#0.3.2 부터since 0.3.2소스source

컬러를 반정밀 실수(RGBA16F, HDR)로 지을지. (false 면 RGBA8, 기본 false)

Whether to build color as half float (RGBA16F, HDR). (RGBA8 when false, default false)

반환Returnsbool

필드fieldpub use_depth_texture: bool#0.3.2 부터since 0.3.2소스source

깊이를 샘플링할 수 있는 텍스처(24 비트)로 지을지. (false 면 깊이 렌더버퍼, use_depth 가 true 일 때만 쓴다, 기본 false)

Whether to build depth as a samplable texture (24-bit). (a depth renderbuffer when false, used only when use_depth is true, default false)

반환Returnsbool

필드fieldpub sample_count: i32#0.4.0 부터since 0.4.0소스source

멀티샘플 표본 수. (2 밑이면 멀티샘플 없음)

Multisample count. (no multisampling below 2)

반환Returnsi32

필드fieldpub depth_bits: i32#0.4.0 부터since 0.4.0소스source

깊이 렌더버퍼 비트 수. (24 면 24, 그 밖은 16)

Depth renderbuffer bit count. (24 for 24, 16 otherwise)

반환Returnsi32

RenderTargetstruct

짝Counterpart: vanilla.js RenderTarget소스source

렌더 타겟.

Render target.

예제Example씬을 HDR 렌더 타겟에 그리고 컬러 텍스처를 다음 패스에서 읽기Draw the scene to an HDR render target and read the color texture in the next pass
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(())
}
생성자constructorpub fn new(gl: &GlContext, width: i32, height: i32, use_depth: bool, options: Option<RenderTargetOptions>) -> Result<RenderTarget, String>#0.3.2 부터since 0.3.2소스source

생성. (options 가 None 이면 기본 옵션)

  • 크기가 0 x 0 이면 GL 객체를 짓지 않는다. (resize 가 같은 크기라 건너뛴다)

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 컨텍스트. (Graphic::get_gl 로 꺼낸다)GL context. (taken out with Graphic::get_gl)
widthi32
가로 크기. (픽셀)Width. (pixels)
heighti32
세로 크기. (픽셀)Height. (pixels)
use_depthbool
깊이 어태치먼트를 붙일지. (렌더버퍼, use_depth_texture 면 깊이 텍스처)Whether to attach a depth attachment. (a renderbuffer, or a depth texture with use_depth_texture)
optionsOption<RenderTargetOptions>
옵션. (None 이면 RenderTargetOptions::default())Options. (RenderTargetOptions::default() when None)

반환ReturnsResult<RenderTarget, String> 새 렌더 타겟. 프레임버퍼, 텍스처, 렌더버퍼를 못 만들면 Err(GL 의 오류 글).New render target. Err(GL error message) if a framebuffer, texture or renderbuffer cannot be created.

짝Counterpart: vanilla.js RenderTarget.constructor

예제Example4x MSAA 로 그리고 resolve 로 텍스처에 풀기Draw with 4x MSAA and resolve into the texture
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(())
}
정적staticpub fn read_supported_sample_count(gl: &GlContext, requested_sample_count: i32, internal_format_list: &[u32]) -> i32#0.4.0 부터since 0.4.0소스source

기기가 받는 표본 수. (요청 수를 MAX_SAMPLES 와 포맷마다 받는 표본 수 밑의 가장 큰 수로, 2 밑이면 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 컨텍스트.GL context.
requested_sample_counti32
바라는 표본 수. (2 밑이면 멀티샘플 없음)Desired sample count. (no multisampling below 2)
internal_format_list&[u32]
같이 쓸 렌더버퍼 포맷 목록. (glow::RGBA8, glow::DEPTH_COMPONENT24 같은 것, 데스크탑 GL 3.3 은 보지 않는다)List of renderbuffer formats to be used together. (such as glow::RGBA8, glow::DEPTH_COMPONENT24, not checked on desktop GL 3.3)

반환Returnsi32 지을 수 있는 표본 수. (2 이상) 요청이 2 밑이거나 받는 수가 2 밑이면 0.Sample count that can be built. (2 or more) 0 if the request is below 2 or the supported count is below 2.

짝Counterpart: vanilla.js RenderTarget.readSupportedSampleCount

예제Example4x MSAA 를 받는지 미리 보기Check ahead whether 4x MSAA is supported
use vanilla::RenderTarget;

fn read_msaa_sample_count(gl: &vanilla::GlContext) -> i32 {
	RenderTarget::read_supported_sample_count(gl, 4, &[glow::RGBA8, glow::DEPTH_COMPONENT16])
}
메서드methodpub fn resize(&mut self, gl: &GlContext, width: i32, height: i32) -> Result<(), String>#0.3.2 부터since 0.3.2소스source

크기 변경. (같은 크기면 무시, 다르면 리소스 재생성)

  • 다시 지으면 앞의 텍스처는 지워지므로 get_color_texture 로 새로 받는다. 멀티샘플 표본 수도 다시 고른다.

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 컨텍스트.GL context.
widthi32
새 가로 크기. (픽셀)New width. (pixels)
heighti32
새 세로 크기. (픽셀)New height. (pixels)

반환ReturnsResult<(), String> 다시 지었거나 같은 크기면 Ok. GL 객체를 못 만들면 Err(GL 의 오류 글, 그때까지 지은 것은 지우고 크기는 0 이다).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.

짝Counterpart: vanilla.js RenderTarget.resize

메서드methodpub fn resolve(&self, gl: &GlContext)#0.4.0 부터since 0.4.0소스source

멀티샘플을 텍스처로 풀기. (컬러 어태치먼트마다와 깊이 텍스처, 묶여 있던 프레임버퍼로 돌림, 원본 resolve())

  • 푼 뒤에도 멀티샘플 색과 깊이는 그대로 남는다. (invalidate_framebuffer 로 버리지 않는다, 아이패드와 아이폰 사파리에서 프레임마다 수 ~ 수십 ms 가 붙고 이득이 재어지지 않았다, 풀고 나서 이어 그리는 것도 된다)

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 컨텍스트.GL context.

짝Counterpart: vanilla.js RenderTarget.resolve

메서드methodpub fn resolve_with_attachment_kept(&self, gl: &GlContext, is_attachment_kept: bool)#0.4.0 부터since 0.4.0소스source

멀티샘플을 텍스처로 풀기. (옛 꼴과 맞추려고 남긴 짝, 원본 resolve(isAttachmentKept))

Resolves multisample into textures. (Kept to match the old shape, original resolve(isAttachmentKept))

gl&GlContext
GL 컨텍스트.GL context.
is_attachment_keptbool
더는 쓰지 않는다. (어느 쪽이든 푼 뒤에 멀티샘플 색과 깊이를 버리지 않는다)No longer used. (Either way the multisample color and depth are not discarded after resolving)

짝Counterpart: vanilla.js RenderTarget.resolve

예제Example풀고 나서 같은 판에 이어 그리기Keep drawing after resolving
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(())
}
메서드methodpub fn bind(&self, gl: &GlContext)#0.3.2 부터since 0.3.2소스source

렌더링 대상으로 바인드. (프레임버퍼 + 뷰포트, 멀티샘플이면 멀티샘플 프레임버퍼)

Binds as the rendering target. (framebuffer + viewport, the multisample framebuffer when multisampled)

gl&GlContext
GL 컨텍스트.GL context.

짝Counterpart: vanilla.js RenderTarget.bind

메서드methodpub fn destroy(&mut self, gl: &GlContext)#0.3.2 부터since 0.3.2소스source

파괴. (GL 리소스 해제)

  • 크기를 0 으로 되돌리므로 그 뒤 resize 로 다시 지을 수 있다.

Destroy. (releases GL resources)

  • The size goes back to 0, so it can be rebuilt with resize afterward.
gl&GlContext
만들 때 쓴 GL 컨텍스트.GL context used at creation.

짝Counterpart: vanilla.js RenderTarget.destroy

메서드methodpub fn get_framebuffer(&self) -> Option<GlFramebuffer>#0.3.2 부터since 0.3.2소스source

프레임버퍼 반환. (그리는 프레임버퍼, 멀티샘플이면 멀티샘플 프레임버퍼)

Returns the framebuffer. (The framebuffer that is drawn into, the multisample framebuffer when multisampled)

반환ReturnsOption<GlFramebuffer> 그리는 프레임버퍼. destroy 뒤나 짓지 않았으면 None.Framebuffer drawn to. None after destroy or if not built.

짝Counterpart: vanilla.js RenderTarget.getFramebuffer

메서드methodpub fn get_resolve_framebuffer(&self) -> Option<GlFramebuffer>#0.4.0 부터since 0.4.0소스source

텍스처가 붙은 프레임버퍼 반환. (멀티샘플이 아니면 get_framebuffer 와 같음)

Returns the framebuffer with the textures attached. (same as get_framebuffer if not multisampled)

반환ReturnsOption<GlFramebuffer> 컬러 텍스처가 붙은 프레임버퍼. destroy 뒤나 짓지 않았으면 None.Framebuffer with the color textures attached. None after destroy or if not built.

짝Counterpart: vanilla.js RenderTarget.getResolveFramebuffer

메서드methodpub fn get_sample_count(&self) -> i32#0.4.0 부터since 0.4.0소스source

실제로 지은 표본 수 반환. (0 이면 멀티샘플 없음)

Returns the sample count actually built. (0 means no multisampling)

반환Returnsi32 멀티샘플 표본 수. (0 이면 멀티샘플 없음, 요청보다 작을 수 있다)Multisample count. (0 means no multisampling, may be smaller than requested)

짝Counterpart: vanilla.js RenderTarget.getSampleCount

메서드methodpub fn get_depth_bits(&self) -> i32#0.4.0 부터since 0.4.0소스source

깊이 렌더버퍼 비트 수 반환. (16 이나 24)

Returns the depth renderbuffer bit count. (16 or 24)

반환Returnsi32 깊이 렌더버퍼 비트 수. (16 이나 24)Depth renderbuffer bit count. (16 or 24)

짝Counterpart: vanilla.js RenderTarget.getDepthBits

메서드methodpub fn get_color_texture(&self, attachment_index: usize) -> Option<GlTexture>#0.3.2 부터since 0.3.2소스source

컬러 텍스처 반환. (어태치먼트 인덱스, 원본 기본값 0)

Returns the color texture. (attachment index, the original default is 0)

attachment_indexusize
컬러 어태치먼트 번호. (0 부터)Color attachment index. (from 0)

반환ReturnsOption<GlTexture> 그 어태치먼트의 컬러 텍스처. 번호가 어태치먼트 수 이상이거나 짓지 않았으면 None.Color texture of that attachment. None if the index is at or above the attachment count or it is not built.

짝Counterpart: vanilla.js RenderTarget.getColorTexture

메서드methodpub fn get_depth_texture(&self) -> Option<GlTexture>#0.3.2 부터since 0.3.2소스source

깊이 텍스처 반환. (use_depth_texture 옵션일 때만 존재)

Returns the depth texture. (exists only with the use_depth_texture option)

반환ReturnsOption<GlTexture> 깊이 텍스처. use_depth 와 use_depth_texture 가 모두 true 가 아니면 None.Depth texture. None unless both use_depth and use_depth_texture are true.

짝Counterpart: vanilla.js RenderTarget.getDepthTexture

메서드methodpub fn get_color_attachment_count(&self) -> usize#0.3.2 부터since 0.3.2소스source

컬러 어태치먼트 개수 반환.

Returns the color attachment count.

반환Returnsusize 컬러 어태치먼트 수. (1 이상)Color attachment count. (1 or more)

짝Counterpart: vanilla.js RenderTarget.getColorAttachmentCount

메서드methodpub fn get_use_float_color(&self) -> bool#0.3.2 부터since 0.3.2소스source

실수 컬러 사용 여부 반환.

Returns whether float color is used.

반환Returnsbool 컬러가 RGBA16F 면 true, RGBA8 이면 false.true if the color is RGBA16F, false if RGBA8.

짝Counterpart: vanilla.js RenderTarget.getUseFloatColor

메서드methodpub fn get_use_depth_texture(&self) -> bool#0.3.2 부터since 0.3.2소스source

깊이 텍스처 사용 여부 반환.

Returns whether the depth texture is used.

반환Returnsbool 옵션의 use_depth_texture.The use_depth_texture option.

짝Counterpart: vanilla.js RenderTarget.getUseDepthTexture

메서드methodpub fn get_width(&self) -> i32#0.3.2 부터since 0.3.2소스source

가로 크기 반환.

Returns the width.

반환Returnsi32 가로 크기. (픽셀, destroy 뒤에는 0)Width. (pixels, 0 after destroy)

짝Counterpart: vanilla.js RenderTarget.getWidth

메서드methodpub fn get_height(&self) -> i32#0.3.2 부터since 0.3.2소스source

세로 크기 반환.

Returns the height.

반환Returnsi32 세로 크기. (픽셀, destroy 뒤에는 0)Height. (pixels, 0 after destroy)

짝Counterpart: vanilla.js RenderTarget.getHeight