deferred shading

28
Deferred Shading http://ohyecloudy.com http://cafe.naver.com/shader.cafe 2009.03.30

Upload: -

Post on 24-May-2015

2.716 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Deferred Shading

Deferred Shading

http://ohyecloudy.com

http://cafe.naver.com/shader.cafe

2009.03.30

Page 2: Deferred Shading

Introduction

• 보통 사용하는 Forward Shading에서는 라이팅 비용이 비싸다.

– 각각의 오브젝트마다 라이팅

– 게임에서 사용하는 라이트 개수가 많아지고 있다.

• G-buffer에 라이팅 재료들을 렌더링하고 라이팅에 오브젝트들 대신 G-buffer를 사용.

– 라이팅 비용을 줄인다.

– Deferred Shading

Page 3: Deferred Shading

Three major options for real-time lighting

• Forward Rendering

– single pass with multiple lights

– multiple passes with multiple lights lighting

• Deferred Rendering

Page 4: Deferred Shading

Single-pass, multi-light

for each object do for each light do framebuffer = light_model(object,light);

Page 5: Deferred Shading

Frame

Buffer

Page 6: Deferred Shading

문제점

• 화면에 렌더링 되지 않을 Geometry도 라이팅 연산을 한다.

• 멀티 라이트일때 관리가 어렵다.

–싱글 셰이더 템플릿을 사용하기 때문에 조합하는 가지수가 폭발적으로 늘어난다.

Page 7: Deferred Shading

Multi-pass, multi-light

for each light do for each object affected by light do framebuffer += light_model(object,light);

Page 8: Deferred Shading

Previous Frame Buffer

Current Frame Buffer

Page 9: Deferred Shading

문제점

• 화면에 렌더링 되지 않을 Geometry도 라이팅 연산을 한다.

• 높은 배치 카운트

– 1/object/light

• 각 패스마다 중복된 작업이 많다.

– Vertex transform & setup

Page 10: Deferred Shading

Deferred Shading

for each object do G-buffer = lighting properties of object; for each light do framebuffer += light_model(G-buffer,light);

Page 11: Deferred Shading

G-Buffer

Previous Frame Buffer

Current Frame Buffer

Page 12: Deferred Shading

특징

• 배치가 갂단해지고 관리가 쉽다

• 그림자 테크닉과 통합이 쉽다.

• 라이팅이 O(1)에 가능

–오브젝트 개수에 상관 없다.

Page 13: Deferred Shading

G-Buffer

• Per-pixel lighting에 필요한 모든 정보

– Normal

– Position

– Diffuse / Specular Albedo, other attributes

Page 14: Deferred Shading

Normal

Page 15: Deferred Shading

Depth

Page 16: Deferred Shading

Diffuse

Page 17: Deferred Shading

Specular factor

Page 18: Deferred Shading

Computing Lighting

For each light: diffuse += diffuse(G-buff.N, L)) specular += G-buff.spec * specular(G-buff.N, G-buff.P, L)

Page 19: Deferred Shading

Diffuse Lighting

Page 20: Deferred Shading

Specular reflection

Page 21: Deferred Shading

Deferred Composition

framebuffer = diffuse * G-buff.diffuse + specular

Page 22: Deferred Shading
Page 23: Deferred Shading

Shader code

• Writing G-buffer

• Lighting

Page 24: Deferred Shading

Writing G-buffer PS_MRT_OUTPUT SimplePS(VS_OUTPUT0 In) { PS_MRT_OUTPUT Out; half4 diffuseTex = tex2D( DiffuseMapSampler, In.TexCoord0); half3 normalTex = tex2D( NormalMapSampler, In.TexCoord1); diffuseTex.xyz = diffuseTex.w; //put normal into view space float3x3 TangentToView; TangentToView[0] = In.TangentToView0; TangentToView[1] = In.TangentToView1; TangentToView[2] = In.TangentToView2; half3 normal = normalize(mul(normalTex, TangentToView)); //pack normal = normal * 0.5f + 0.5f; Out.Color0 = float4(diffuseTex.xyz, NotShadowed); Out.Color1 = float4(normal, 0.0); Out.Color2 = float4(In.Position2.z / In.Position2.w, 0.0f, 0.0f, 0.0f); return Out; }

Page 25: Deferred Shading

float4 DirLightingPS(VS_OUTPUT1 In) : COLOR { half4 diffuseTex = tex2D(MRT0Sampler, In.TexCoord); half4 normalTex = tex2D(MRT1Sampler, In.TexCoord); half z = tex2D(MRT2Sampler, In.TexCoord); //reconstruct original view-space position float x = In.Position2.x; float y = In.Position2.y; float4 position = mul(float4(x, y, z, 1.0), InvProj); position.xyz = position.xyz / position.www; position.w = 1.0f; //compute position in light space … //diffuse lighting finalLighting = shadow * diffuse + Ambient + emissive; return float4(finalLighting, 1.0f); }

Per-Pixel Lighting

Page 26: Deferred Shading

극복해야 할 사항

• Transparency

– alpha blending

– G-buffer는 하나의 픽셀만 저장하기 때문에 Forward Rendering처럼 갂단하지 않다.

• Anti-Aliasing

– Forward Rendering처럼 frame buffer에 직접 렌더링할때 자동으로 받는 하드웨어 anti-aliasing이 불가능

Page 27: Deferred Shading

결론

• O(1)에 Lighting을 할 수 있다.

–오브젝트 개수에 상관없이 상수 시갂에 라이팅이 가능하다.

• 사용하는 렌더 타겟의 개수가 늘어나는게 부담.

• Transparency, AA 등 극복해야 하는 문제점들이 존재.