closing the distance between cpu and gpu with signed

23
d Closing the Distance Between CPU and GPU with Signed Distance Fields Arjen Hiemstra

Upload: others

Post on 17-Feb-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Closing the Distance Between CPU and GPU with Signed

default

Closing the Distance Between CPU andGPU with Signed Distance Fields

Arjen Hiemstra

Page 2: Closing the Distance Between CPU and GPU with Signed

default

Next up...

1 Some Concepts

2 Signed Distance Fields

3 Use Cases

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 2/24

Page 3: Closing the Distance Between CPU and GPU with Signed

default

2D Rendering: The Postscript Model

Dominant 2D renderingmodel for at least 30 years.Essentially models a 2Dplotter.Used by major APIs likeQPainter and Canvas.

moveTo(10, 10)

lineTo(10, 30)

lineTo(30, 30)

lineTo(30, 10)

lineTo(30, 10)

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 3/24

Page 4: Closing the Distance Between CPU and GPU with Signed

default

3D Rendering: The GPU

Pratically every moderncomputer has one.Massively parallel.Originally designed toaccelerate 3D.

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 4/24

Page 5: Closing the Distance Between CPU and GPU with Signed

default

Programmable Shaders

Small programs that getexecuted by the GPU.Written in GLSL or othershading language.Various stages existcorresponding to differentparts of pipeline.

1 // Vertex Shader2 uniform mat4 modelViewProjection;3 in vec4 vertexPosition;4 void main()5 {6 gl_Position = vertexPosition *

modelViewProjection;7 }89 // Fragment Shader

10 out vec4 color;11 void main()12 {13 color = vec4(1.0, 0.0, 0.0, 1.0);14 }

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 5/24

Page 6: Closing the Distance Between CPU and GPU with Signed

default

Next up...

1 Some Concepts

2 Signed Distance Fields

3 Use Cases

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 6/24

Page 7: Closing the Distance Between CPU and GPU with Signed

default

The Problem

It would be nice if we could use the GPU to acceleraterendering complex 2D items.

But The PostScript model is inherently serial, while the GPU ismassively parallel.

So We need a new rendering model for 2D shapes.

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 7/24

Page 8: Closing the Distance Between CPU and GPU with Signed

default

Example: Circle

sdf (~p) = ‖~p‖ − r

Where:~p is a 2D vector in range 〈−1,1〉r is the radius of the circle.

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 8/24

Page 9: Closing the Distance Between CPU and GPU with Signed

default

Turning Math to Image

sdf (~p) = ‖~p‖ − r

-1.0 1.0

-1.0

1.0

r = 0.5

AB

C

A ~p = 0.00.5 Distance = 0.0

B ~p = 0.750.75 Distance = 0.56

C ~p = −0.25−0.25 Distance = -0.15

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 9/24

Page 10: Closing the Distance Between CPU and GPU with Signed

default

On the GPU

1 uniform float radius;2 uniform vec4 color;34 in vec2 uv;56 out vec4 out_color;78 void main()9 {

10 float distance = length(uv) -radius;

11 out_color = vec4(distance);12 }

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 10/24

Page 11: Closing the Distance Between CPU and GPU with Signed

default

An Actual Circle

1 uniform float radius;2 uniform vec4 color;34 in vec2 uv;56 out vec4 out_color;78 void main()9 {

10 float distance = length(uv) -radius;

11 out_color = distance > 0.0 ?12 vec4(0.0) : color;13 }

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 11/24

Page 12: Closing the Distance Between CPU and GPU with Signed

default

Antialiasing

1 uniform float radius;2 uniform vec4 color;34 in vec2 uv;56 out vec4 out_color;78 void main()9 {

10 float distance = length(uv) -radius;

11 out_color = mix(color, vec4(0.0),12 clamp(distance * 100.0, 0.0,

1.0));13 }

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 12/24

Page 13: Closing the Distance Between CPU and GPU with Signed

default

Operations

Annular Round Outline

Union Subtract Intersect

Translate Rotate Scale

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 13/24

Page 14: Closing the Distance Between CPU and GPU with Signed

default

Next up...

1 Some Concepts

2 Signed Distance Fields

3 Use Cases

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 14/24

Page 15: Closing the Distance Between CPU and GPU with Signed

default

Rendering Circles

Rendering circles is a tricky problem in 3D graphics.Standard geometric approach provides an approximation.

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 15/24

Page 16: Closing the Distance Between CPU and GPU with Signed

default

Pie Charts

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 16/24

Page 17: Closing the Distance Between CPU and GPU with Signed

default

Line Charts

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 17/24

Page 18: Closing the Distance Between CPU and GPU with Signed

default

Kirigami’s Cards

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 18/24

Page 19: Closing the Distance Between CPU and GPU with Signed

default

ShadowedRectangle

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 19/24

Page 20: Closing the Distance Between CPU and GPU with Signed

default

ShadowedTexture

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 20/24

Page 21: Closing the Distance Between CPU and GPU with Signed

default

New Cards

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 21/24

Page 22: Closing the Distance Between CPU and GPU with Signed

default

Putting It Together

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 22/24

Page 23: Closing the Distance Between CPU and GPU with Signed

default

Closing

Special Thanks To:Inigo Quilez

https://iquilezles.org/

Links

KQuickCharts https://invent.kde.org/framework/kquickcharts/

Kirigami https://invent.kde.org/framework/kirigami/

Author Arjen HiemstraMatrix: @ahiemstra:kde.org

Closing the Distance Between CPU and GPU with Signed Distance Fields — Arjen Hiemstra 23/24