All we do in frontend development ends up as pixels on screen yet many people don't really know how a pixel goes from zeroes and ones in memory to bright colours on screen.
This talk explains how that works and gives an introduction into the basic principles of 2D and 3D computer graphics, filtering and different techniques to make things look better.
This can help you improve your rendering performance not only for games, but regular websites, too.
28. @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
29. @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
35. @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)
39. @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...
40. @g33konaut
Canvas time!
● we are on a separate layer
● we get direct pixel access
● yet there’s performance considerations!
41. @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
48. @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
49. @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.