LearnMaking demos

Lesson 29 of 30 · 50 min

Fast and small

Learn what makes shaders slow, speed them up, and balance brevity with clarity

In this lesson you will

  • Understand that a shader's cost is the product of pixels × work per pixel × repetitions
  • Measure and compare cost with Frame time in /edit and by coloring the step count
  • Speed things up by exiting early, skipping work with bounds, coarsening the distance and hoisting work out of loops
  • Know how to pass the posting checks (8 ms, 12 KB, loops up to 64, no gl_FragCoord)
  • Write small code with #define and short names, and judge the balance with readability

01Cost is a multiplication

A shader runs the same function for every pixel on the screen. So the cost of one frame is roughly a multiplication: number of pixels × work per pixel × number of repetitions. Halve any one of them and the whole thing halves. When speeding things up, the basic question is which of the three you can shrink.

Let's put numbers on it. A 1920×1080 full screen has about 2.07 million pixels. In a raymarched work, each pixel calls map up to 64 times for marching, 6 times for the normal, up to 32 times for the shadow and 5 times for AO: more than 100 calls in total.

1

2

3

// map calls per frame (full screen, raymarched work, a generous estimate)

// 2,073,600 pixels × (64 + 6 + 32 + 5) calls ≈ 220,000,000

// with a 5-layer fBm inside map, that's 5× as many noise calls, and 4× more hashes

GPUs are very fast, but at 60 frames per second you only have about 16.7 ms per frame. This site's posting check aims for 8 ms per frame, half of that, to leave room for the rest of the page and for slower devices. Going full screen makes a work heavier for the same reason: the "number of pixels" factor grows.

The example below adds up glowing particles in a loop. The particle count COUNT is the "number of repetitions" directly. Going from 16 to 64 makes the picture busier, but it also quadruples the work.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

Starts when it scrolls into view
Open in /edit ↗
▸Set COUNT to 64 for four times the particles. Use "Open in /edit" below to take it to /edit, and compare the Frame time at 16 and at 64

Tip

Loop limits must be written as numbers, so when you want an adjustable count, give the loop a generous numeric limit (64 here) and leave early with if (i >= COUNT) break;. A #define name like COUNT is replaced by its number before compiling.

02Measuring in /edit

The previews on these lesson pages don't show cost. To measure, press "Open in /edit" under a playground to take the code to /edit. Under the /edit preview you'll see the code size (bytes), frames drawn per second (fps) and the elapsed time, and opening "Pre-publish checks" below that shows the five checks run before posting.

  • Compiles … the code compiles
  • Size … the code is 12 KB or less, comments included
  • Frame time … one frame takes 8 ms or less, shown like "3.2 ms / 8 ms budget"
  • No gl_FragCoord … the code doesn't use gl_FragCoord (positions come in as uv and p)
  • Loops bounded … every for has a numeric limit of 64 or less

Once all five pass, "Continue to Submit" lets you post. Frame time is re-measured continuously while the preview runs, so keep these in mind when reading it:

  • The number wobbles from frame to frame; watch for a few seconds and read the typical value
  • Watch the whole animation; it may get heavy only when the camera faces an expensive part
  • Change one thing at a time and compare, so you know what actually helped
  • It depends on the preview size; full screen means more pixels and more cost
  • Watch fps as well: if it drops well below your screen's refresh rate (60 on most screens), the work is too heavy

Watch out

Speed varies many times over between GPUs, and Frame time is measured on your device. If your computer is fast, aim well under 8 ms (say, about half) so the work still runs smoothly for people on laptops and phones.

03Seeing the steps

In a raymarched work, the "number of repetitions" is the number of steps a ray takes before it hits something. As in Raymarching, coloring the step count shows at a glance where the time goes. Below, blue means few steps and red means the 64-step limit.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Starts when it scrolls into view
Open in /edit ↗
▸The sky (top half) is solid red: rays that hit nothing walk all 64 steps every time. The rims of the spheres and a distant band of ground are red too

There are two kinds of red. One is the sky: nothing is ever hit, so rays keep walking to the limit. The other is where rays graze a surface, like the rims of objects or the distant ground. When grazing, the distance function returns small values, so the ray can only creep forward and the steps pile up.

04Exiting early

The easiest way to cut steps is to leave the loop early. There are two conditions:

  • Stop when the ray has gone too far (t > 20.0). Walking further than the fog lets anyone see is wasted work
  • Loosen the "hit" test with distance (d < 0.001 * t). Distant things are small on screen, so precision finer than a pixel changes nothing you can see

In the example below, the left half uses the same condition as before and the right half adds both conditions. Both halves show the step count as color.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

Starts when it scrolls into view
Open in /edit ↗
▸The sky on the right is now blue: it passes 20 within a few steps and leaves. Change t > 20.0 to t > 8.0 and even the distant ground turns blue, though you also see less of the world

The band of grazing ground stays red even on the right. Rays there really can only creep forward. Such bands are often hidden in fog anyway, so choosing the cut-off distance where the fog gets thick trims the waste.

Tip

A GPU runs groups of neighboring pixels together, in lockstep. If one pixel in a group keeps looping, the others wait for it. Early exits pay off best when a large area, like the sky, can leave early all together.

05Skipping work with bounds

Say your distance function contains something expensive, like fine bumps. At points far from the object, those bumps don't matter at all. So first measure the distance to a simple shape that fully encloses the object (a sphere, say), and while that's far, just return it. The enclosing sphere is bigger than what's inside, so stepping that far can never pass through the object.

The example below counts how many times the expensive detail function is called and shows it as color. The left half doesn't use a bound; the right half does.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

Starts when it scrolls into view
Open in /edit ↗
▸On the left, even rays looking at the ground or sky call detail at every step. On the right, only rays passing right by the sphere call it. Change 1.2 to 1.0 and the bound becomes smaller than the bumps, which is incorrect and can leave holes

Watch out

A bounding shape's distance is smaller than the true distance. That's safe for marching, but the soft shadows and AO from Shadows and depth use the distance values themselves, so a bound can slightly change how wide or dark they look. If the look changes, give shadows and AO a map without the bound, or fit the bound more tightly.

06Coarser far away

Distant things are small on screen, so fine detail there gets squashed into single pixels. In that case you may as well skip the fine work far away. This idea is called LOD (level of detail).

The mountains below build their height by layering noise as in Layering noise (fBm). Within 5 units of the camera they use 6 layers, up to 12 units 4 layers, and beyond that only 2. So that map knows the distance to the camera, the camera position goes into a global variable (one declared outside any function), camPos, written once at the start of render.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

Starts when it scrolls into view
Open in /edit ↗
▸Change the 4 and 2 in dist < 5.0 ? 6 : (dist < 12.0 ? 4 : 2) to 6 to use 6 layers all the way out. It hardly looks different, but the work grows a lot. Compare the Frame time in /edit

Where the layer count switches, the shape of the mountains can change slightly (an effect called popping). If it bothers you, push the switch distance back into the thick fog, or fade the last layer's weight smoothly to 0 with distance.

07Hoisting work out of loops

map gets called close to 100 times per pixel. If it contains a calculation that gives the same answer every call, you're repeating the same work 100 times. For example, rot(time * 0.7) is the same matrix however often you compute it, because time doesn't change during a frame. Do such work once in render, store it in a global variable, and reuse it. This is called hoisting it out of the loop.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// before: four cos and sin calls on every map call

float map(vec3 p) {

vec3 q = p;

q.xz = rot(time * 0.7) * q.xz;

q.xy = rot(time * 0.4) * q.xy;

return sdBox(q, vec3(0.35));

}

 

// after: build them once in render, and only use them in map

mat2 spinA;

mat2 spinB;

float map(vec3 p) {

vec3 q = p;

q.xz = spinA * q.xz;

q.xy = spinB * q.xy;

return sdBox(q, vec3(0.35));

}

// at the start of render: spinA = rot(time * 0.7); spinB = rot(time * 0.4);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

Starts when it scrolls into view
Open in /edit ↗
▸Change the 0.7 in rot(time * 0.7) to 2.0 for a faster spin. As an experiment, change q.xz = spinA * q.xz; back to q.xz = rot(time * 0.7) * q.xz; and compare the Frame time in /edit

Anything else that's the same on every pass can come out too: normalize on a light direction, color table calculations, a camera position that depends only on time. Calculations whose answer depends on p (like a rotation that differs from place to place) can't come out. The trick is to ask yourself: does this value change from pixel to pixel, or from step to step?

Tip

There are small tricks too: x * x is faster than pow(x, 2.0), and if you only need to compare lengths, dot(v, v) (the squared length) can replace length(v). But the big wins come from cutting how many times things run, as in the sections above. Look at steps and counts first.

08Writing small

Now for code size. Posted code can be up to 12 KB (12,288 bytes), comments included. Letters and digits are 1 byte each, but Japanese characters take 3 bytes, so lots of Japanese comments add up quickly. Still, 12 KB is plenty, and most works never need to worry.

Even so, getting a beautiful picture out of very little code is one of the joys of shaders. The Gallery's "Shortest code" sort lists such works. Here are the tricks for writing short:

  • Delete unused functions and code you commented out while experimenting (this helps most)
  • Name repeated patterns with #define; it can even take arguments, as in #define R(a) mat2(cos(a), sin(a), -sin(a), cos(a))
  • Shorten names (color → c) and combine declarations (float r = length(p), a = atan(p.y, p.x);)
  • Write numbers short: 0.5 can be .5 and 2.0 can be 2.; inside constructors like vec3(…), plain integers like 0 and 1 are fine
  • Trim spaces, line breaks and comments

1

2

3

4

5

6

7

// readable (107 bytes)

float glow = 0.02 / abs(length(p) - 0.5);

vec3 color = vec3(1.0, 0.6, 0.3) * glow;

return vec4(color, 1.0);

 

// short (51 bytes)

return vec4(vec3(1,.6,.3)*.02/abs(length(p)-.5),1);

Below is a whole work of about 230 bytes, written with #define and short names. It folds and rotates the coordinates 8 times (the same folding idea as in Fractals) and adds a glowing ring each time.

1

2

3

4

5

6

7

8

9

Starts when it scrolls into view
Open in /edit ↗
▸Change -.4 to -.3 and .2 to .15 and the pattern changes completely. Change i<8 to i<5 for fewer folds and a larger pattern

Watch out

#define simply pastes in its text. Without surrounding parentheses, as in #define W(x) .5+.5*cos(x), 2.*W(a) turns into 2.*.5+.5*cos(a), which isn't what you meant. Wrap expression macros in (…).

09Balancing size and readability

Short code is hard to read later. On this site, your code appears on the work page as is, and others remix it with "Open in editor". And when you come back to fix something weeks later, code full of c, q and k is unreadable. Decide how far to shrink based on what the work is for.

  • For most works, favor readability. If you near 12 KB, trim unused functions and comments first
  • Gather tuning numbers (steps, focal length, colors) as #defines at the top: shorter and clearer at once
  • Short names that shaders use everywhere, like p, d, t and col, are perfectly readable as they are
  • If smallness is the point of the work, go all out; just keep a readable version for yourself

10Exercises

The second exercise is the heart of this lesson. To speed something up without changing how it looks, hunt for where the same work is done, and how many times. Measuring the Frame time in /edit as you go shows you what each change buys.

Exercise 1

A raymarch that exits early

This work moves through a forest of pillars. The loop has no exit condition, so every pixel walks all 64 steps every time. Without changing the look, add two conditions: leave when the ray hits, and leave when it has gone too far. Remove the // from // col = heat(steps / 64.0); just above return to check the step count as color.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

Starts when it scrolls into view
Open in /edit ↗
Exercise 2

Lighten a heavy shader

A bumpy planet floats over the ground. The code follows the posting rules, but three kinds of wasted work are hiding in it and slowing it down. Find and fix them without changing the look. Open it in /edit and compare the Frame time before and after (results depend on your device, but the difference is clear where there's lots of sky).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

Starts when it scrolls into view
Open in /edit ↗
Exercise 3

Half the size

Rewrite this work of spreading rays and rings so the code is half its size in bytes or less, without changing the look. Don't cram it onto one line, though: keep line breaks and indentation so it stays reasonably readable. Open it in /edit to see the size in bytes under the preview.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Starts when it scrolls into view
Open in /edit ↗

11Recap

  • Cost is pixels × work per pixel × repetitions; shrink any one and the total shrinks
  • Check Frame time (up to 8 ms) and Size (up to 12 KB) in /edit's Pre-publish checks
  • Coloring the step count reveals where time goes: the sky and grazing angles
  • Exit early (d < 0.001 * t || t > max), skip work with bounds, coarsen the distance, hoist repeated work
  • To write small, start with unused functions and comments; wrap #define expressions in (…)
  • Usually favor readability: your code is shown on the work page and gets remixed

Next is the final lesson. We'll polish composition, color and motion into a finished demo, then post and export it so you can share your work.

Finished lessons get a mark in the course contents