realtime computer graphics on gpus - univerzita karlovakolomaznik/doc/framebuffer.pdf ·...

37
Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on GPUs Framebuffer and Offscreen Rendering Techniques Jan Kolomazn´ ık Department of Software and Computer Science Education Faculty of Mathematics and Physics Charles University in Prague March 12, 2020 1 / 37

Upload: others

Post on 29-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

Realtime Computer Graphics on GPUsFramebuffer and Offscreen Rendering Techniques

Jan Kolomaznı́k

Department of Software and Computer Science EducationFaculty of Mathematics and Physics

Charles University in Prague

March 12, 2020

1 / 37

Page 2: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

Introduction

2 / 37

Page 3: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

DEFINITIONS AND HISTORY

I Framebuffer, screen buffer, video buffer, . . .I Memory containing bitmap driving video displayI 70s – framebuffers big enough to contain standard video

imageI Atari 2600 – Racing the beamI HW support for sprites, shifting the framebuffer (scrolling), . . .

3 / 37

Page 4: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

DOUBLE BUFFERING

I Also known as page flippingFront buffer – currently visibleBack buffer – currently rendered off-screen

I Requires fast buffer swapI Prevents

I screen tearingI flickeringI render artefacts

4 / 37

Page 5: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

Framebuffer Structure

5 / 37

Page 6: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

FRAMEBUFFER

I Default framebuffer created with window creationI Custom off-screen framebuffer:

I Can choose resolutionI Arbitrary attachmentsI Render to textureI Filtering, postprocessingI Interoperability with other APIs (CUDA, OpenCL, . . . )

6 / 37

Page 7: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

FRAMEBUFFER ATTACHMENTS

I 2D rendering targetI Almost any object containing image or image arrayI For complex objects specify what part to attach:

Cube map select face3D texture z-slice

Mipmap choose a level. . .

I Specify semantics – how it will be used in the renderingpipeline

7 / 37

Page 8: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

COLOR ATTACHMENTS

I Should match fragment shader outputsI Color:

I 1-4 channelsI Integer (8-32), floatI Special storage types: GL R3 G3 B2, GL RGB10 A2, . . .

I Color updated on successful pass through all fragment tests

8 / 37

Page 9: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

DEPTH BUFFER (Z-BUFFER)

I Contains depth information for each pixelI Solves visibility problem

I Geometry can be streamedI Works only for opaque objects

I Precision depends on:I z-buffer element typeI projection – decreasing precision with increasing distance

(choose proper near/far clipping planes)

9 / 37

Page 10: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

STENCIL BUFFER

I Additional buffer with integer elementsI Usually shares memory with z-bufferI Limits area for rendering – stencilingI Often used for shadow computationI Can be updated by results of stencil and depth testI Behavior setup:

glStencilFunc: what the test doesglStencilOp: what happens on test pass/fail

10 / 37

Page 11: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

OPERATIONS AND TESTS ON FRAGMENTS

I Scissor testI Alpha testI Depth testI Stencil testI BlendingI DitheringI Logical operations (only integer based colors)

11 / 37

Page 12: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

DEPTH TEST

I Different conditions for different objects (e.g. outline hiddenobjects)

I glDepthFunc()I GL NEVER, GL ALWAYSI GL LESS, GL EQUAL, GL LEQUAL, . . .

I Z-fighting – z-buffer precisionI glPolygonOffset()

I Modulate z-value for specified primitivesI Early depth test optimization

12 / 37

Page 13: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

ALPHA TEST

I void glAlphaFunc(GLenum func, GLclampf ref);I Reference value and comparison functionI RGBA mode – fragment accepted/rejected by the alpha testI By default, ref is zero, func is GL ALWAYSI func: GL ALWAYS, GL NEVER, GL LESS, GL EQUAL,

GL LEQUAL, GL GEQUAL, GL GREATER or GL NOTEQUALI glEnable(GL ALPHA TEST);

13 / 37

Page 14: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

COLOR BLENDING

I How the color of the pixel is updated by fragment shaderoutput

I Render transparent objects –I disable depth test, painters algorithm (order primitives)I order independent transparency – depth peeling

I glBlendFunc() mixing colors based on their respective alphavalues.

I The source color: the color of the fragment be drawn.I The destination color: the color already present in the color

buffer. Blending functions are specified using glBlendFunc,which takes two parameters:

14 / 37

Page 15: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

ANTIALIASING

I Supersampling (SSAA)I Render in higher resolutionI Show downsampled image – smoothing

I Multisampling (MSAA)I Multiple depth/stencil tests per pixelI Estimates fragment coverage – smoothing on edges

15 / 37

Page 16: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

RENDER BUFFER VS. TEXTURE

Best buffer for framebuffer attachments?I Render buffer object:

I contains image, which will not be sampled (read)I optimized as render targetI support MSAA

I Textures:I optimized for read accessI can be used later in the rendering pipeline

16 / 37

Page 17: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

TRIPLE BUFFERING AND V-SYNC

I V-Sync: new frame is rendered in sync with monitor refreshfrequency (60-100 Hz)

I Double buffering + V-Sync – small interval when none of thebuffers can be touched – delay, idle GPU

I Second backbuffer – no delays, highest possible framerateI Meaningful only when refresh rate lower than maximal

possible rendering framerate

17 / 37

Page 18: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

Shadows

18 / 37

Page 19: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW CASTING

I Static Shadows: baked light/shadow mapI Dynamic shadows:

I single shadow-receiving planeI simple approach, not generally usable

I shadow mappingI shadow depth-buffer, supported in HW – shadowmap sampler

I shadow volumesI precise but very computationally intensive

I sharp shadows (one pass)I soft shadows (more passes, accumulation of results)

19 / 37

Page 20: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW RECEIVING PLANE

I sharp shadows – point light sourceI use of stencil buffer and multiple scene passesI stencil prevents shadow duplicationI simple algorithmI single shadow-receiving planeI shadow could be opaque (destroying the original surface

color) or transparent (only reducing the amount of light)

20 / 37

Page 21: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW RECEIVING PLANE – PROCEDURE

1. the whole scene rendered using ordinary projectionI shadow-receiver sets stencil to 1I other objects zero this bit

2. potential shadow-casters rendered to the shadow-receivingplaneI depth-test is offI special projection matrixI shadows are drawn only to the (stencil==1) pixels

21 / 37

Page 22: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

FACE CULLING

I From the point of view of cameraI GPU can filter (face cull) according to vertex order:

I glEnable( GL CULL FACE );I glFrontFace( GL CCW );I glCullFace( GL BACK ); // draw front faces only

I Speed optimization

22 / 37

Page 23: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW VOLUME – DEPTH PASS

I shadow-caster – infinite shadow volume from countour(shadow solid)

I lateral faces of a shadow solid are considered, but invisibleI virtual ray from the camera is tested against these facesI GPU can rasterize the virtual faces and ”draw” them into the

stencil bufferI Front faces increase stencilI Back faces decrease stencil

I stencil buffer values define shadows in the sceneI has to be done separately for each point light source

23 / 37

Page 24: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW VOLUME – DEPTH PASS

24 / 37

Page 25: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW VOLUME – DEPTH PASS

25 / 37

Page 26: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW VOLUME – DEPTH FAIL

I Carmack’s reverseI camera can be placed anywhereI shadow solid sealed using ”caps”: one is illuminated part of an

object, the second one in infinityI second phase: lateral shadow faces and both ”caps”

I Front faces – decrement on depth failI Back faces – increment on depth fail

I third phase: stencil==0 means ”light”

26 / 37

Page 27: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW VOLUME – DEPTH FAIL

27 / 37

Page 28: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW VOLUME – DEPTH FAIL

28 / 37

Page 29: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW MAPPING

1. scene is rendered from the light-source viewpointI no need to modify frame buffer, only depth-buffer has to be

updated2. depth-buffer is moved into a texture (”shadow map”)

I regular projection according to the cameraI use of projective texture coordinatesI test actual distance of a fragment from the light source (in the

world space) against shadow-map texture

29 / 37

Page 30: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW MAPPING

30 / 37

Page 31: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SHADOW MAPPING PROBLEMS

I Shadow acneI Perspective aliasingI Sharp shadowsI Hard to choose optimal size of shadow maps

I Solution: cascaded shadow maps

31 / 37

Page 32: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

Deffered Shading

32 / 37

Page 33: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

BOTTLENECKS IN RASTERIZATION PIPELINE

I Processing lots of lightsI Complicated materialsI Lots of fragments shaded and not used

33 / 37

Page 34: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

DEFFERED SHADING

I Decouple geometry and light processingI Two stages:

1. Render geometry to textures – multiple render targets(G-buffer)

2. Posprocessing – apply light computations

34 / 37

Page 35: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

COMPOSITING STEP

I Compute shader or draw one fullscreen quadI Apply lighting for only visible fragmentsI All shading parameters come from uniforms and texturesI Modern engines do postprocessing

I Motion blurI Depth of fieldI Screen space ambient occlusionI Screen space decalsI BloomI HDR processing

35 / 37

Page 36: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

DISADVANTAGES

I Cannot handle transparency (depth peeling)I Complicated usage of multiple material typesI Memory intensiveI MSAA does not work:

I SupersamplingI Smoothing trick (small scale, rotate with linear interpolation,

. . . )I Postprocessing – edge detection and masked smoothing,

morphological AA (MLAA)

36 / 37

Page 37: Realtime Computer Graphics on GPUs - Univerzita Karlovakolomaznik/doc/framebuffer.pdf · Introduction Framebuffer Structure Shadows Deffered Shading Realtime Computer Graphics on

Introduction Framebuffer Structure Shadows Deffered Shading

SUMMARY: OPENGL CALLS

Framebuffer setup: glGenFramebuffers, glBindFramebuffer,glGenRenderbuffers, glFramebufferTexture*,glBlitFramebuffer, glRenderbufferStorageMultisample

Z-buffer and stencil buffer: glDepthFunc, glStencilMask,glStencilFunc, glStencilOp, glPolygonOffset

Other: glBlendEquation, glBlendFunc, glScissor,

37 / 37