Slide 1

Slide 1 text

GRAPHICS KOTLIN
 for @romainguy

Slide 2

Slide 2 text

Android KTX

Slide 3

Slide 3 text

Dealing with legacy

Slide 4

Slide 4 text

// Pack a color int val color = ((a and 0xff) shl 24) or ((r and 0xff) shl 16) or ((g and 0xff) shl 8) or ((b and 0xff) ) // Unpack a color int val a = (color shr 24) and 0xff val r = (color shr 16) and 0xff val g = (color shr 8) and 0xff val b = (color ) and 0xff

Slide 5

Slide 5 text

inline val @receiver:ColorInt Int.alpha get() = (this shr 24) and 0xff inline val @receiver:ColorInt Int.red get() = (this shr 16) and 0xff inline val @receiver:ColorInt Int.green get() = (this shr 8) and 0xff inline val @receiver:ColorInt Int.blue get() = (this ) and 0xff

Slide 6

Slide 6 text

val a = color.alpha val r = color.red val g = color.green val b = color.blue

Slide 7

Slide 7 text

inline operator fun @receiver:ColorInt Int.component1() = this.alpha inline operator fun @receiver:ColorInt Int.component2() = this.red inline operator fun @receiver:ColorInt Int.component3() = this.green inline operator fun @receiver:ColorInt Int.component4() = this.blue

Slide 8

Slide 8 text

val (a, r, g, b) = color

Slide 9

Slide 9 text

Destructure everything

Slide 10

Slide 10 text

// Points val (x, y) = PointF(1.0f, 2.0f) + PointF(3.0f, 4.0f) // Rectangles val (l, t, r, b) = Rect(0, 0, 4, 4) and Rect(2, 2, 6, 6)

Slide 11

Slide 11 text

// Points val (x, y) = PointF(1.0f, 2.0f) + PointF(3.0f, 4.0f) // Rectangles val (l, t, r, b) = Rect(0, 0, 4, 4) and Rect(2, 2, 6, 6) // Matrix val (right, up, forward, eye) = viewMatrix

Slide 12

Slide 12 text

// Points
 val (x, y) = PointF(1.0f, 2.0f) + PointF(3.0f, 4.0f)
 
 // Rectangles
 val (l, t, r, b) = Rect(0, 0, 4, 4) and Rect(2, 2, 6, 6)
 // Matrix
 val (right, up, forward, eye) = viewMatrix val (x, y, z) = eye

Slide 13

Slide 13 text

Implement operators

Slide 14

Slide 14 text

Kotlin Arithmetic operators Set operators plus + ∪ (union) minus – – (difference) times × div / or ∪ (union) and ∩ (intersection) xor ⊖ (symmetric difference) not U \ (complement)

Slide 15

Slide 15 text

// Union val r = RectF(0.0f, 0.0f, 4.0f, 4.0f) or RectF(2.0f, 2.0f, 6.0f, 6.0f) // Symmetric difference val r = Rect(0, 0, 4, 4) xor Rect(2, 2, 6, 6) // Difference val path = circle - square // Offset val (l, t, r, b) = Rect(0, 0, 2, 2) + 2 val (l, t, r, b) = Rect(0, 0, 2, 2) - Point(1, 2)

Slide 16

Slide 16 text

inline operator fun Rect.contains(p: PointF) = contains(p.x, p.y)

Slide 17

Slide 17 text

if (PointF(x, y) in path.bounds) { // Hit detected }

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

inline operator fun Bitmap.get(x: Int, y: Int) = getPixel(x, y)

Slide 20

Slide 20 text

inline operator fun Bitmap.get(x: Int, y: Int) = getPixel(x, y)

Slide 21

Slide 21 text

inline operator fun Bitmap.get(x: Int, y: Int) = getPixel(x, y) inline operator fun Bitmap.set(x: Int, y: Int, @ColorInt color: Int) =
 setPixel(x, y, color)

Slide 22

Slide 22 text

val b = getBitmap() val a = b[1, 1].alpha if (a < 255) { b[1, 1] = 0xff_00_00_00 }

Slide 23

Slide 23 text

inline operator fun Bitmap.get(x: IntRange, y: IntRange): IntArray

Slide 24

Slide 24 text

bitmap[16..32, 16..32].forEach { val (_, r, g, b) = it }

Slide 25

Slide 25 text

All together

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

if (bounds.contains(hit.x, hit.y)) {

Slide 28

Slide 28 text

if (bounds.contains(hit.x, hit.y)) { val pixel = Bitmap.createBitmap(width, height, ARGB_8888).let {

Slide 29

Slide 29 text

if (bounds.contains(hit.x, hit.y)) { val pixel = Bitmap.createBitmap(width, height, ARGB_8888).let { with(Canvas(it)) { save() scale(2.0f, 2.0f) val path = Path().apply { op(path1, path2, DIFFERENCE) } drawPath(path, paint) restore() }

Slide 30

Slide 30 text

if (bounds.contains(hit.x, hit.y)) { val pixel = Bitmap.createBitmap(width, height, ARGB_8888).let { with(Canvas(it)) { save() scale(2.0f, 2.0f) val path = Path().apply { op(path1, path2, DIFFERENCE) } drawPath(path, paint) restore() } it.getPixel(hit.x, hit.y) }

Slide 31

Slide 31 text

if (bounds.contains(hit.x, hit.y)) { val pixel = Bitmap.createBitmap(width, height, ARGB_8888).let { with(Canvas(it)) {
 save()
 scale(2.0f, 2.0f)
 val path = Path().apply { op(path1, path2, DIFFERENCE) }
 drawPath(path, paint)
 restore()
 } it.getPixel(hit.x, hit.y)
 } val r = Color.red(pixel)
 val g = Color.green(pixel)
 val b = Color.blue(pixel)
 }

Slide 32

Slide 32 text

if (hit in bounds) { val (_, r, g, b) = createBitmap(width, height).applyCanvas { withScale(2.0f, 2.0f) { drawPath(path1 - path2, paint) } }[hit.x, hit.y] }

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

vec3 color = vec3(0.65, 0.85, 1.0) + direction.y * 0.72; // (distance, material) vec2 hit = traceRay(origin, direction); float distance = hit.x; float material = hit.y; // We've hit something in the scene if (material > 0.0) { vec3 lightDir = vec3(0.6, 0.7, -0.7); vec3 position = origin + distance * direction; vec3 v = normalize(-direction); vec3 n = normal(position); vec3 l = normalize(lightDir); vec3 h = normalize(v + l); vec3 r = normalize(reflect(direction, n)); float NoV = abs(dot(n, v)) + 1e-5; float NoL = saturate(dot(n, l)); float NoH = saturate(dot(n, h)); float LoH = saturate(dot(l, h)); // ... }

Slide 35

Slide 35 text

var color = Float3(0.65f, 0.85f, 1.0f) + direction.y * 0.72f // (distance, material) val hit = traceRay(origin, direction) val distance = hit.x val material = hit.y // We've hit something in the scene if (material > 0.0f) { val lightDir = Float3(0.6f, 0.7f, -0.7f) val position = origin + distance * direction val v = normalize(-direction) val n = normal(position) val l = normalize(lightDir) val h = normalize(v + l) val r = normalize(reflect(direction, n)) val NoV = abs(dot(n, v)) + 1e-5f val NoL = saturate(dot(n, l)) val NoH = saturate(dot(n, h)) val LoH = saturate(dot(l, h)) // ... }

Slide 36

Slide 36 text

Data first Math is functional

Slide 37

Slide 37 text

data class Float3(var x: Float = 0.0f, var y: Float = 0.0f, var z: Float = 0.0f) + operators

Slide 38

Slide 38 text

inline fun abs(v: Float3) = Float3(abs(v.x), abs(v.y), abs(v.z)) inline fun length(v: Float3) = sqrt(v.x * v.x + v.y * v.y + v.z * v.z) inline fun length2(v: Float3) = v.x * v.x + v.y * v.y + v.z * v.z inline fun distance(a: Float3, b: Float3) = length(a - b) inline fun dot(a: Float3, b: Float3) = a.x * b.x + a.y * b.y + a.z * b.z

Slide 39

Slide 39 text

fun lookAt( eye: Float3, target: Float3, up: Float3 = Float3(z = 1.0f) ): Mat4 { val f = normalize(target - eye)
 val r = normalize(f x up)
 val u = normalize(r x f)
 return Mat4(r, u, f, eye)
 }

Slide 40

Slide 40 text

fun lookAt( eye: Float3, target: Float3, up: Float3 = Float3(z = 1.0f) ): Mat4 { val f = normalize(target - eye)
 val r = normalize(f x up)
 val u = normalize(r x f)
 return Mat4(r, u, f, eye)
 } val h = normalize(v + l) val r = normalize(reflect(direction, n)) val NoV = abs(dot(n, v)) + 1e-5f val NoL = saturate(dot(n, l))

Slide 41

Slide 41 text

Aliasing & swizzling

Slide 42

Slide 42 text

vec4 v = vec4(…) // Use XYZ for coordinates vec3 position = v.xyz // Use RGB for color data vec3 color = v.rgb // Swizzling vec3 reverseColor = v.bgr
 vec3 grayColor = v.ggg vec4 twoPositions = v.xyxy

Slide 43

Slide 43 text

// In Float4 inline var xy: Float2 get() = Float2(x, y) set(value) { x = value.x y = value.y } inline var rg: Float2 get() = Float2(x, y) set(value) { x = value.x y = value.y }

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

enum class VectorComponent { X, Y, Z, W, R, G, B, A, S, T, P, Q }

Slide 46

Slide 46 text

enum class VectorComponent { X, Y, Z, W, R, G, B, A, S, T, P, Q } operator fun get(index: VectorComponent) = when (index) { VectorComponent.X, VectorComponent.R, VectorComponent.S -> x VectorComponent.Y, VectorComponent.G, VectorComponent.T -> y VectorComponent.Z, VectorComponent.B, VectorComponent.P -> z else -> throw IllegalArgumentException(“Unknown index”) }

Slide 47

Slide 47 text

enum class VectorComponent {
 X, Y, Z, W,
 R, G, B, A,
 S, T, P, Q
 } 
 operator fun get(index: VectorComponent) = when (index) {
 VectorComponent.X, VectorComponent.R, VectorComponent.S -> x
 VectorComponent.Y, VectorComponent.G, VectorComponent.T -> y
 VectorComponent.Z, VectorComponent.B, VectorComponent.P -> z
 else -> throw IllegalArgumentException(“Unknown index”)
 } 
 operator fun get(
 index1: VectorComponent,
 index2: VectorComponent,
 index3: VectorComponent
 ): Float3 {
 return Float3(get(index1), get(index2), get(index3))
 }

Slide 48

Slide 48 text

val v = Float4(…)
 val position = v.xyz
 val color = v.rgb
 val reverseColor = v[B, G, R]
 val grayColor = v[G, G, G] val twoPositions = v[X, Y, X, Y]

Slide 49

Slide 49 text

Row major Column major

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

right

Slide 52

Slide 52 text

right up

Slide 53

Slide 53 text

right up forward

Slide 54

Slide 54 text

right up forward translation

Slide 55

Slide 55 text

val transform: Mat4 // 3rd column, first element val x3 = transform[2, 0]
 val x3 = transform.z.x // Math notation // Row-major, 1-based val x3 = transform(1, 3)

Slide 56

Slide 56 text

Where there is one there are many

Slide 57

Slide 57 text

if (color.r > 0.0f && color.g > 0.0f && color.b > 0.0f) { // ... }

Slide 58

Slide 58 text

if (all(color gt Float3(0.0f))) { // ... }

Slide 59

Slide 59 text

if (all(color gt Float3(0.0f))) { // ... } // any() for || if (any(color lt black)) { // ... }

Slide 60

Slide 60 text

https://github.com/google/filament

Slide 61

Slide 61 text

https://github.com/google/filament

Slide 62

Slide 62 text

Local extension functions

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

private fun createMesh() { data class Vertex(val x: Float, val y: Float, val z: Float, val n: Float3)

Slide 65

Slide 65 text

private fun createMesh() { data class Vertex(val x: Float, val y: Float, val z: Float, val n: Float3) fun ByteBuffer.put(v: Vertex): ByteBuffer { putFloat(v.x) putFloat(v.y) putFloat(v.z) v.n.forEach { putFloat(it) } return this }

Slide 66

Slide 66 text

private fun createMesh() { data class Vertex(val x: Float, val y: Float, val z: Float, val n: Float3) fun ByteBuffer.put(v: Vertex): ByteBuffer { putFloat(v.x) putFloat(v.y) putFloat(v.z) v.n.forEach { putFloat(it) } return this } val vertexData = ByteBuffer.allocate(vertexCount * vertexSize) .order(ByteOrder.nativeOrder()) // Face -Z .put(Vertex(-1.0f, -1.0f, -1.0f, Float3(0.0f, 0.0f, -1.0f))) .put(Vertex(-1.0f, 1.0f, -1.0f, Float3(0.0f, 0.0f, -1.0f))) // ...

Slide 67

Slide 67 text

private fun createMesh() {
 data class Vertex(val x: Float, val y: Float, val z: Float, val n: Float3)
 fun ByteBuffer.put(v: Vertex): ByteBuffer {
 putFloat(v.x)
 putFloat(v.y)
 putFloat(v.z)
 v.n.forEach { putFloat(it) }
 return this
 }
 val vertexData = ByteBuffer.allocate(vertexCount * vertexSize)
 .order(ByteOrder.nativeOrder())
 // Face -Z
 .put(Vertex(-1.0f, -1.0f, -1.0f, Float3(0.0f, 0.0f, -1.0f)))
 .put(Vertex(-1.0f, 1.0f, -1.0f, Float3(0.0f, 0.0f, -1.0f)))
 // ...
 // Build mesh with vertexData
 }

Slide 68

Slide 68 text

1.3

Slide 69

Slide 69 text

Unsigned Integers

Slide 70

Slide 70 text

UInt

Slide 71

Slide 71 text

UInt represents natural numbers ℕ

Slide 72

Slide 72 text

@ColorInt operator fun Bitmap.get(x: Int, y: Int): Int

Slide 73

Slide 73 text

@ColorInt operator fun Bitmap.get(x: Int, y: Int): Int // Can x and y be < 0? // Will it throw, clamp or wrap around? myBitmap[-1, -1]

Slide 74

Slide 74 text

@ColorInt operator fun Bitmap.get(x: UInt, y: UInt): Int

Slide 75

Slide 75 text

@ColorInt operator fun Bitmap.get(x: UInt, y: UInt): Int // No more ambiguity myBitmap[-1, -1]

Slide 76

Slide 76 text

@ColorInt operator fun Bitmap.get(x: UInt, y: UInt): Int // No more ambiguity myBitmap[-1, -1] Conversion of signed constants to unsigned ones is prohibited

Slide 77

Slide 77 text

Kotlin Signed Byte Short Int Long Unsigned Char UInt ULong

Slide 78

Slide 78 text

JVM/ART Signed byte short int long Unsigned char

Slide 79

Slide 79 text

val a: UInt = //... val b: UInt = //... val c = a + b

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

1: iload 4 // load a

Slide 82

Slide 82 text

1: iload 4 // load a 2: iload 5 // load b

Slide 83

Slide 83 text

1: iload 4 // load a 2: iload 5 // load b 3: iadd

Slide 84

Slide 84 text

1: iload 4 // load a 2: iload 5 // load b 3: iadd 4: invokestatic #13 // kotlin/UInt."constructor-impl":(I)I

Slide 85

Slide 85 text

1: iload 4 // load a 2: iload 5 // load b 3: iadd 4: invokestatic #13 // kotlin/UInt."constructor-impl":(I)I 5: istore_3

Slide 86

Slide 86 text

4: invokestatic #13 // kotlin/UInt."constructor-impl":(I)I

Slide 87

Slide 87 text

public static int constructor-impl(int); Code: 0: iload_0 1: ireturn

Slide 88

Slide 88 text

1: iload 4 // load a 2: iload 5 // load b 3: isub 4: invokestatic #13 // kotlin/UInt."constructor-impl":(I)I 5: istore_3

Slide 89

Slide 89 text

1: iload 4 // load a 2: iload 5 // load b 3: imul 4: invokestatic #13 // kotlin/UInt."constructor-impl":(I)I 5: istore_3

Slide 90

Slide 90 text

1: iload 4 // load a 2: iload 5 // load b 3: invokestatic #91 // kotlin/UnsignedKt."uintDivide-J1ME1BU":(II)I 4: invokestatic #13 // kotlin/UInt."constructor-impl":(I)I 5: istore_3

Slide 91

Slide 91 text

UInt

Slide 92

Slide 92 text

UInt

Slide 93

Slide 93 text

UInt —— — — — — — — — —

Slide 94

Slide 94 text

Inline Classes

Slide 95

Slide 95 text

Inline classes are a zero-cost* abstraction

Slide 96

Slide 96 text

Inline classes are a zero-cost* abstraction * When used right

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

• Wraps a single value

Slide 99

Slide 99 text

• Wraps a single value • The underlying value is read-only

Slide 100

Slide 100 text

• Wraps a single value • The underlying value is read-only • Can only implement interfaces

Slide 101

Slide 101 text

• Wraps a single value • The underlying value is read-only • Can only implement interfaces • equals/hashcode/toString for free

Slide 102

Slide 102 text

• Wraps a single value • The underlying value is read-only • Can only implement interfaces • equals/hashcode/toString for free • Cannot be used as vararg* * Unless you are UInt/ULong

Slide 103

Slide 103 text

inline class Color(private val c: Int) { val a: Int get() = (c shr 24) and 0xff val r: Int get() = (c shr 16) and 0xff val g: Int get() = (c shr 8) and 0xff val b: Int get() = (c ) and 0xff }

Slide 104

Slide 104 text

fun printColor(color: Color) { println(""" red = ${color.r / 255f} green = ${color.g / 255f} blue = ${color.b / 255f} alpha = ${color.a / 255f} """.trimIndent()) } val color = Color(0x7f_10_20_30) printColor(color)

Slide 105

Slide 105 text

val color = Color(0x7f_10_20_30)

Slide 106

Slide 106 text

0: ldc #163 // int 2131763248 val color = Color(0x7f_10_20_30)

Slide 107

Slide 107 text

0: ldc #163 // int 2131763248 1: invokestatic #164 // Method Color."constructor-impl":(I)I val color = Color(0x7f_10_20_30)

Slide 108

Slide 108 text

0: ldc #163 // int 2131763248 1: invokestatic #164 // Method Color."constructor-impl":(I)I 2: istore 7 val color = Color(0x7f_10_20_30)

Slide 109

Slide 109 text

0: ldc #163 // int 2131763248 1: invokestatic #164 // Method Color."constructor-impl":(I)I 2: istore 7 3: iload 7 val color = Color(0x7f_10_20_30)

Slide 110

Slide 110 text

0: ldc #163 // int 2131763248 1: invokestatic #164 // Method Color."constructor-impl":(I)I 2: istore 7 3: iload 7 4: invokestatic #166 // Method "printColor-M0a6fYQ":(I)V val color = Color(0x7f_10_20_30)

Slide 111

Slide 111 text

fun printColor(color: Color)

Slide 112

Slide 112 text

public static final void printColor-M0a6fYQ(int); 0: iload_0 fun printColor(color: Color)

Slide 113

Slide 113 text

public static final void printColor-M0a6fYQ(int);
 0: iload_0 1: invokestatic #112 // Method Color.”getR-impl":(I)I fun printColor(color: Color)

Slide 114

Slide 114 text

public static final void printColor-M0a6fYQ(int);
 0: iload_0 1: invokestatic #112 // Method Color.”getR-impl":(I)I fun printColor(color: Color) public static final int getR-impl(int); 0: iload_0 1: bipush 16 2: ishr 3: sipush 255 4: iand 5: ireturn

Slide 115

Slide 115 text

Be careful with auto-boxing

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

• Nullable types

Slide 118

Slide 118 text

• Nullable types • Inside collections, arrays, etc.

Slide 119

Slide 119 text

• Nullable types • Inside collections, arrays, etc. • When Object or Any is expected

Slide 120

Slide 120 text

val a = Color(0x7f_10_20_30) val b = Color(0x7f_30_20_10)

Slide 121

Slide 121 text

val a = Color(0x7f_10_20_30) val b = Color(0x7f_30_20_10) println(a == b)

Slide 122

Slide 122 text

val a = Color(0x7f_10_20_30)
 val b = Color(0x7f_30_20_10)
 println(a == b) println(a.equals(b))

Slide 123

Slide 123 text

a == b

Slide 124

Slide 124 text

a == b

Slide 125

Slide 125 text

1: iload_2 a == b

Slide 126

Slide 126 text

1: iload_2 2: invokestatic #152 // Method Color."box-impl":(I)LColor; a == b

Slide 127

Slide 127 text

1: iload_2 2: invokestatic #152 // Method Color."box-impl":(I)LColor; 3: iload_1 a == b

Slide 128

Slide 128 text

1: iload_2 2: invokestatic #152 // Method Color."box-impl":(I)LColor; 3: iload_1 4: invokestatic #152 // Method Color."box-impl":(I)LColor; a == b

Slide 129

Slide 129 text

1: iload_2 2: invokestatic #152 // Method Color."box-impl":(I)LColor; 3: iload_1 4: invokestatic #152 // Method Color."box-impl":(I)LColor; 5: invokestatic #163 // Method kotlin/jvm/internal/Intrinsics.
 //.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z a == b

Slide 130

Slide 130 text

a.equals(b)

Slide 131

Slide 131 text

a.equals(b)

Slide 132

Slide 132 text

1: iload_2 a.equals(b)

Slide 133

Slide 133 text

1: iload_2 2: iload_1 a.equals(b)

Slide 134

Slide 134 text

1: iload_2 2: iload_1 3: invokestatic #152 // Method Color."box-impl":(I)LColor; a.equals(b)

Slide 135

Slide 135 text

1: iload_2 2: iload_1 3: invokestatic #152 // Method Color."box-impl":(I)LColor; 4: invokestatic #156 // Method Color."equals-impl":(ILjava/lang/Object;)Z a.equals(b)

Slide 136

Slide 136 text

inline class Color(private val value: Int) { val a: Int get() = (value shr 24) and 0xff val r: Int get() = (value shr 16) and 0xff val g: Int get() = (value shr 8) and 0xff val b: Int get() = (value ) and 0xff }

Slide 137

Slide 137 text

inline class Color(val value: Int) { val a: Int get() = (value shr 24) and 0xff val r: Int get() = (value shr 16) and 0xff val g: Int get() = (value shr 8) and 0xff val b: Int get() = (value ) and 0xff }

Slide 138

Slide 138 text

val a = Color(0x7f_10_20_30) val b = Color(0x7f_30_20_10)

Slide 139

Slide 139 text

val a = Color(0x7f_10_20_30)
 val b = Color(0x7f_30_20_10)
 println(a.value == b.value)

Slide 140

Slide 140 text

var s = “” // Boxing!
 s += a 
 // No boxing
 s += a.toString()

Slide 141

Slide 141 text

Coroutines

Slide 142

Slide 142 text

Render Culling 0 N Sorting Shadow Color GC Lights 0 N 0 N 0 N 0 N 0 N Driver N

Slide 143

Slide 143 text

private fun startRender(image: BufferedImage, viewer: ImageViewer) { GlobalScope.launch { while (true) { runBlocking { renderTiles(image, viewer) } } } }

Slide 144

Slide 144 text

private val NumCpu = Runtime.getRuntime().availableProcessors() private val TilesDispatcher = newFixedThreadPoolContext(NumCpu * 3, "Renderer") private suspend fun renderTiles(image: BufferedImage, viewer: ImageViewer) { // ... coroutineScope { repeat(NumCpu) { i -> repeat(NumCpu) { j -> // ... launch(TilesDispatcher) { val pixels = renderTile(x, y, w, h, resolution, time) viewer.pushUpdate(x, height - h - y, w, h, pixels) } } } } }

Slide 145

Slide 145 text

Kotlin scripting

Slide 146

Slide 146 text

My wishlist

Slide 147

Slide 147 text

User-defined literals

Slide 148

Slide 148 text

inline class Half(private val s: Short) { fun toFloat(): Float = // ... }

Slide 149

Slide 149 text

inline class Half(private val s: Short) { fun toFloat(): Float = // ... } fun Float.toHalf(): Half = // ...

Slide 150

Slide 150 text

inline class Half(private val s: Short) {
 fun toFloat(): Float = // ...
 } 
 fun Float.toHalf(): Half = // ... 
 suffix operator fun Float._h() = this.toHalf()

Slide 151

Slide 151 text

// pi is of type Half val pi = 3.1415_h

Slide 152

Slide 152 text

// 2.0_s returns 2,000 ms suffix operator fun Float._ns() = this / 1_000f suffix operator fun Float._ms() = this suffix operator fun Float._s() = this * 1_000f suffix operator fun Float._m() = this * 60_000f suffix operator fun Float._h() = this * 3_600_000f

Slide 153

Slide 153 text

Short-form constructors

Slide 154

Slide 154 text

data class Vec3(x: Float, y: Float, z: Float) fun lookAt(pos: Vec3) = // ... lookAt([1f, 2f, 3f])

Slide 155

Slide 155 text

data class Vertex(pos: Float3, normal: Float3) addVertex([[1f, 2f, 3f], [0f, 0f, 1f]])

Slide 156

Slide 156 text

Linear allocations

Slide 157

Slide 157 text

Kotlin/GPU

Slide 158

Slide 158 text

Where to find some code kotlin-math
 https://github.com/romainguy/kotlin-math filament
 https://github.com/google/filament https://google.github.io/filament/Filament.md.html kotlin-shadertoy https://goo.gl/TFKUwP

Slide 159

Slide 159 text

Talks this week Mathematical modeling with Kotlin
 Today @13:00 Build a game using libGDX and Kotlin
 Today @15:15 Porting D3.js to Kotlin Multiplatform
 Tomorrow @13:00

Slide 160

Slide 160 text

Questions?