Martin Naumann "Life of a pixel: Web rendering performance"

50
@g33konaut What happens on the GPU doesn’t stay on the GPU Life of a pixel

Transcript of Martin Naumann "Life of a pixel: Web rendering performance"

Page 1: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

What happens on the GPU doesn’t stay on the GPULife of a pixel

Page 2: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Доброго ранку, Київ!Як поживаєте?

Page 3: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

ZURICH, SWITZERLAND - yeah!

Page 4: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

But before we look into browsers...

Page 5: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Software development is hard...

Page 6: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Things go wrong...

Page 7: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

...a lot...

Page 8: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

… seriously!

Page 9: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Why do we put up with this?

Page 10: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

We do it for our users!

Page 11: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

And we’re not alone!

Page 12: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

… and we’ve got many, many tools!

Page 13: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

So much choice can be puzzling...

Page 14: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

… because we don’t wanna go wrong ...

Page 15: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Here. It. Comes.

Page 16: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

It’s mostly choice & flavour

Page 17: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Creating user value >

Page 18: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Some are simple

Page 19: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Some are complex

Page 20: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Some require … effort ...

Page 21: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

It’s about people, learning & fun!

Please keep it like that.

Page 22: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Anyway.

Let’s fight Jank!

Page 23: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

First things first: What the flip is a pixel?

RGB

Page 24: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Make it sparkle!

Memory010101110010101010010010101010010010010101010010010011101010101010101011010110101110010011101010101111011

Page 28: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

GPU, CPU, OMG, WTF?

CPU

● general purpose● multitasking is a must● data dependencies● assumption:

I/O slower than calculations

GPU

● specialised for graphics● no dependencies● highly parallel● assumption:

unidirectional data flow

Page 29: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Why should we not use JS for drawing stuff?

1920 x 1080 pixels = 2’073’600 pixels

24bit color = 3 byte per pixel = 6’220’800 byte = ~5,3 MB

Page 30: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Hey, main thread,

please draw 2 million pixels in 10ms, kthxbye.

Page 31: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Hey, CPU, ...

Page 32: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Hey, GPU, ...

Page 33: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Don’t worry, Browsers are clever

Page 34: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Efficient painting in browsers: Tiles

Page 35: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Tiles: How?

● page is organised in layers● layers may be broken down into tiles● tiles are saved as bitmaps & uploaded into VRAM● tiles can very often be reused (minimise/maximise, scroll)

Page 36: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Aaaand action!

Page 37: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

CSS Triggers: csstriggers.com

Page 38: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Contain changes with layers JSbin

Page 39: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Make everything a layer! Or… should we?

● layers require VRAM, which is often limited● tiny overhead at creation time: Upload into VRAM● things get very, very slow when you overflow VRAM● you have no idea how much VRAM there is...

Page 40: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Canvas time!

● we are on a separate layer● we get direct pixel access● yet there’s performance considerations!

Page 41: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Canvas tips

● don’t do a lot of text rendering● avoid scaling images in canvas● avoid a large amount of bit copies (i.e. sweeping fillRect)● minimise the amount of draw calls (i.e. combine paths)● try to change as few pixels as possible between frames

Page 42: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Canvas examplerequestAnimationFrame(function draw()

{

x = (x+1) % 300;

ctx.fillStyle = '#000';

ctx.fillRect(0, 0, 300, 300)

ctx.fillStyle = '#f00';

ctx.fillRect(x, 50, 50, 50)

requestAnimationFrame(draw)

})

Page 43: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Canvas examplerequestAnimationFrame(function draw()

{

x = (x+1) % 301;

ctx.fillStyle = '#000';

ctx.fillRect(x-1, 50, 1, 50)

ctx.fillStyle = '#f00';

ctx.fillRect(x, 50, 50, 50)

requestAnimationFrame(draw)

})

Page 45: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

WebGL basically gives youparallel pixel access

Page 46: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Plus: You can program the GPU

Page 47: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

WebGL pipeline (simplified)

Page 48: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Shaders: Vertex Shader & Fragment Shader

● Vertex shaders○ IN: Vertex ○ OUT: transformed vertex○ can do things like projection, waves, fish eye lens, etc.

● Fragment shaders○ IN: screen position, material, light, normal, textures, …○ OUT: Colour, depth value○ can do things like reflection, shading, lens flares, HDR

Page 49: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Summary● Browsers are clever ● Do as much on the GPU, not the CPU● Beware of Layout & Paint● Canvas = CPU-bound pixel access● WebGL = GPU-bound pixel access● Think of your users & colleagues● Don’t panic.

Page 50: Martin Naumann "Life of a pixel: Web rendering performance"

@g33konaut

Дякую!

Slides: bit.ly/fwd16-performance

Twitter: @g33konaut

Web: archilogic.com