Skip to content

Commit 519696b

Browse files
authored
update for webgpu update (#8)
* initial start of updating docs to webgpu * rewrite a bunch of articles for the new webgpu update and remove deprecated info * remove the-game-class * fix some deprecated api calls * remove frame-buffer-objects and replace with a simple render-targets * update mesh and fix links from /lehaine * update nodes.md * update containers.md * tweaks for update * more tweaks
1 parent 263240d commit 519696b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+667
-677
lines changed

_data/navigation.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ docs:
2121
children:
2222
- title: "First application"
2323
url: /docs/starting/first-application
24+
- title: "First render"
25+
url: /docs/starting/first-render
2426
- title: "Template project"
2527
url: /docs/starting/template-project
2628
- title: "Example game"
@@ -29,8 +31,6 @@ docs:
2931
children:
3032
- title: "Context and ContextListener"
3133
url: /docs/overview/context-and-contextlistener
32-
- title: "The Game Class"
33-
url: /docs/overview/the-game-class
3434
- title: "Logging"
3535
url: /docs/overview/logging
3636
- title: "2D Graphics"
@@ -59,8 +59,8 @@ docs:
5959
url: /docs/2d/particles
6060
- title: "2D Meshes"
6161
url: /docs/2d/meshes
62-
- title: "Frame Buffer Objects"
63-
url: /docs/2d/frame-buffer-objects
62+
- title: "Render Targets"
63+
url: /docs/2d/render-targets
6464
- title: "LDtk"
6565
url: /docs/2d/tilemaps/ldtk
6666
- title: "Tiled"
@@ -129,8 +129,6 @@ docs:
129129
children:
130130
- title: "Shaders"
131131
url: /docs/shading/shaders
132-
- title: "GLSL Generator"
133-
url: /docs/shading/glsl-generator
134132
- title: "Multi-threading / Coroutines"
135133
children:
136134
- title: "Coroutines and Threads"
@@ -141,5 +139,5 @@ docs:
141139
url: /docs/tools/texture-packer
142140
- title: "Debugging"
143141
children:
144-
- title: "OpenGL Profiling"
145-
url: /docs/debugging/opengl-profiling
142+
- title: "WebGPU Profiling"
143+
url: /docs/debugging/webgpu-profiling

_docs/2d/animations.md

Lines changed: 107 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ permalink: /docs/2d/animations
55

66
## Animation
77

8-
The [Animation](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/graphics/Animation.kt) class contains the info on the key frames of the what is to be animated. All this class holds are the list of key frames, the list of indices associated to each key frame in the order to be palyed back in, as well as a list of `Duration` frame times for the amount of time spent displaying each frame.
8+
The [Animation](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/graphics/Animation.kt) class contains the info on the key frames of the what is to be animated. All this class holds are the list of key frames, the list of indices associated to each key frame in the order to be palyed back in, as well as a list of `Duration` frame times for the amount of time spent displaying each frame.
99

1010
The `Animation<T>` class also expects a type parameter. This would usually be a [TextureSlice](/docs/2d/textures-and-textureslices) but we can pass in any type we want to animate. For the following examples, we will be using a `TextureSlice`.
1111

12-
The `Animation` class itself doesn't handle any of the playback or playback times. We have to pass in the time expired to the class for it to determine which key frame is to be played. We can use an [AnimationPlayer](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/graphics/AnimationPlayer.kt) to enable playback of an animation but it is not required.
12+
The `Animation` class itself doesn't handle any of the playback or playback times. We have to pass in the time expired to the class for it to determine which key frame is to be played. We can use an [AnimationPlayer](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/graphics/AnimationPlayer.kt) to enable playback of an animation but it is not required.
1313

1414
And of course we can create our own instance of an animation by simply just passing in the list of frames, indices, and times without needing an atlas.
1515

@@ -57,15 +57,45 @@ val heroRun: Animation<TextureSlice> = atlas.getAnimation("heroRun")
5757
val anim = AnimaationPlayer<TextureSlice>()
5858
anim.playLooped(heroRun)
5959

60-
onRender { dt ->
61-
gl.clear(ClearBufferMask.COLOR_BUFFER_BIT)
62-
anim.update(dt) // handles requesting the next frames in the current animation
63-
camera.update()
64-
batch.use(camera.viewProjection) { batch ->
60+
onUpdate { dt ->
61+
val surfaceTexture = graphics.surface.getCurrentTexture()
62+
val swapChainTexture = checkNotNull(surfaceTexture.texture)
63+
val frame = swapChainTexture.createView()
64+
65+
val commandEncoder = device.createCommandEncoder()
66+
val renderPassEncoder =
67+
commandEncoder.beginRenderPass(
68+
desc =
69+
RenderPassDescriptor(
70+
listOf(
71+
RenderPassColorAttachmentDescriptor(
72+
view = frame,
73+
loadOp = LoadOp.CLEAR,
74+
storeOp = StoreOp.STORE,
75+
clearColor =
76+
if (preferredFormat.srgb) Color.DARK_GRAY.toLinear()
77+
else Color.DARK_GRAY
78+
)
79+
)
80+
)
81+
)
82+
batch.use(renderPassEncoder, camera.viewProjection) { batch ->
6583
anim.currentKeyFrame?.let { // use the current key frame of the animation to render
6684
batch.draw(it, 50f, 50f, scaleX = 5f, scaleY = 5f)
6785
}
6886
}
87+
88+
renderPassEncoder.end()
89+
val commandBuffer = commandEncoder.finish()
90+
91+
device.queue.submit(commandBuffer)
92+
graphics.surface.present()
93+
94+
commandBuffer.release()
95+
renderPassEncoder.release()
96+
commandEncoder.release()
97+
frame.release()
98+
swapChainTexture.release()
6999
}
70100
```
71101

@@ -84,15 +114,47 @@ val anim = AnimaationPlayer<TextureSlice>()
84114
anim.playOnce(heroIdle) // stops after one play through
85115
anim.play(heroIdle, times = 5) // stops after 5 play throughs
86116

87-
onRender { dt ->
88-
gl.clear(ClearBufferMask.COLOR_BUFFER_BIT)
117+
onUpdate { dt ->
89118
anim.update(dt)
90119
camera.update()
91-
batch.use(camera.viewProjection) { batch ->
120+
121+
val surfaceTexture = graphics.surface.getCurrentTexture()
122+
val swapChainTexture = checkNotNull(surfaceTexture.texture)
123+
val frame = swapChainTexture.createView()
124+
125+
val commandEncoder = device.createCommandEncoder()
126+
val renderPassEncoder =
127+
commandEncoder.beginRenderPass(
128+
desc =
129+
RenderPassDescriptor(
130+
listOf(
131+
RenderPassColorAttachmentDescriptor(
132+
view = frame,
133+
loadOp = LoadOp.CLEAR,
134+
storeOp = StoreOp.STORE,
135+
clearColor =
136+
if (preferredFormat.srgb) Color.DARK_GRAY.toLinear()
137+
else Color.DARK_GRAY
138+
)
139+
)
140+
)
141+
)
142+
batch.use(renderPassEncoder, camera.viewProjection) { batch ->
92143
anim.currentKeyFrame?.let {
93144
batch.draw(it, 50f, 50f, scaleX = 5f, scaleY = 5f)
94145
}
95146
}
147+
renderPassEncoder.end()
148+
val commandBuffer = commandEncoder.finish()
149+
150+
device.queue.submit(commandBuffer)
151+
graphics.surface.present()
152+
153+
commandBuffer.release()
154+
renderPassEncoder.release()
155+
commandEncoder.release()
156+
frame.release()
157+
swapChainTexture.release()
96158
}
97159
```
98160

@@ -158,11 +220,7 @@ anim.apply {
158220
registerState(heroIdle, 0) // returns true by default.
159221
}
160222

161-
162-
onRender { dt ->
163-
gl.clearColor(Color.CLEAR)
164-
gl.clear(ClearBufferMask.COLOR_BUFFER_BIT)
165-
223+
onUpdate { dt ->
166224
if (input.isKeyJustPressed(Key.NUM1)) {
167225
shouldSleep = !shouldSleep
168226
}
@@ -189,13 +247,45 @@ onRender { dt ->
189247
anim.play(heroRoll, 2.seconds)
190248
}
191249

192-
193250
anim.update(dt)
194251
camera.update()
195-
batch.use(camera.viewProjection) { batch ->
252+
253+
val surfaceTexture = graphics.surface.getCurrentTexture()
254+
val swapChainTexture = checkNotNull(surfaceTexture.texture)
255+
val frame = swapChainTexture.createView()
256+
257+
val commandEncoder = device.createCommandEncoder()
258+
val renderPassEncoder =
259+
commandEncoder.beginRenderPass(
260+
desc =
261+
RenderPassDescriptor(
262+
listOf(
263+
RenderPassColorAttachmentDescriptor(
264+
view = frame,
265+
loadOp = LoadOp.CLEAR,
266+
storeOp = StoreOp.STORE,
267+
clearColor =
268+
if (preferredFormat.srgb) Color.DARK_GRAY.toLinear()
269+
else Color.DARK_GRAY
270+
)
271+
)
272+
)
273+
)
274+
batch.use(renderPassEncoder, camera.viewProjection) { batch ->
196275
anim.currentKeyFrame?.let {
197276
batch.draw(it, 50f, 50f, scaleX = 5f, scaleY = 5f)
198277
}
199278
}
279+
renderPassEncoder.end()
280+
val commandBuffer = commandEncoder.finish()
281+
282+
device.queue.submit(commandBuffer)
283+
graphics.surface.present()
284+
285+
commandBuffer.release()
286+
renderPassEncoder.release()
287+
commandEncoder.release()
288+
frame.release()
289+
swapChainTexture.release()
200290
}
201291
```

_docs/2d/bitmap-fonts.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bitmap Fonts
33
permalink: /docs/2d/fonts/bitmap-fonts
44
---
55

6-
Bitmap fonts are supported in the [BMFont](https://www.angelcode.com/products/bmfont/) format. A [BitmapFont](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/graphics/font/BitmapFont.kt) can draw strings of text using its own internal cache or we can create our own cache using the specified font.
6+
Bitmap fonts are supported in the [BMFont](https://www.angelcode.com/products/bmfont/) format. A [BitmapFont](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/graphics/font/BitmapFont.kt) can draw strings of text using its own internal cache or we can create our own cache using the specified font.
77

88
## Bitmap Font
99

@@ -17,7 +17,7 @@ val font: BitmapFont = resourcesVfs["arial_32.fnt"].readBitmapFont()
1717

1818
### Drawing Text
1919

20-
Each `BitmapFont` object contains its own internal [BitmapFontCache](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/graphics/font/BitmapFontCache.kt) which is an extension of a [FontCache](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/graphics/font/FontCache.kt). When we draw directly with the font, it will store the geometry of the glyphs drawn internally. This will clear any existing text. If we want to draw strings of text at different positions and colors we will need to create our own `BitmapFontCache`.
20+
Each `BitmapFont` object contains its own internal [BitmapFontCache](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/graphics/font/BitmapFontCache.kt) which is an extension of a [FontCache](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/graphics/font/FontCache.kt). When we draw directly with the font, it will store the geometry of the glyphs drawn internally. This will clear any existing text. If we want to draw strings of text at different positions and colors we will need to create our own `BitmapFontCache`.
2121

2222
Multi-line texts, wrapping, and horizontally aligning text are all supported. Wrapping works by specifying the max width that the area of a font should take up. If it exceeds it, the internal glyph layout will wrap the text.
2323

@@ -27,7 +27,7 @@ Multi-line texts, wrapping, and horizontally aligning text are all supported. Wr
2727
val batch = SpriteBatch(context)
2828
val font: BitmapFont = resourcesVfs["arial_32.fnt"].readBitmapFont()
2929

30-
batch.use {
30+
batch.use(renderPass)
3131
// we don't actually have to specify each parameter as most have default options.
3232
font.draw(batch, "This is my test", x = 5f, y = 5f, rotation = Angle.ZERO, color = Color.WHITE, targetWidth = 0f, align = HAlign.LEFT, wrap = false)
3333
}
@@ -48,7 +48,7 @@ val cache = BitmapFontCache(font)
4848
cache.addText("This is my test", x = 5f, y = 5f)
4949
cache.addText("This is another line", x = 50f, y = 5f)
5050

51-
batch.use {
51+
batch.use(renderPass)
5252
// draws any stored text data
5353
cache.draw(batch)
5454
}
@@ -61,12 +61,11 @@ val batch = SpriteBatch(context)
6161
val font: BitmapFont = resourcesVfs["arial_32.fnt"].readBitmapFont()
6262
val cache = BitmapFontCache(font)
6363

64-
6564
cache.addText("This is my first line", x = 50f, y = 5f)
6665

6766
cache.setText("This is my test", x = 5f, y = 5f) // this will clear the data from the 'addText' method.
6867

69-
batch.use {
68+
batch.use(renderPass)
7069
// draws any stored text data
7170
cache.draw(batch) // will only draw "This is my test"
7271
}
@@ -88,7 +87,7 @@ Once we are done we can call the `dispose()` method on the `BitmapFont` object.
8887
```kotlin
8988
val font: BitmapFont = resourcesVfs["arial_32.fnt"].readBitmapFont()
9089

91-
onDispose {
92-
font.dispose() // font is no longer usable
90+
onRelease {
91+
font.release() // font is no longer usable
9392
}
9493
```

_docs/2d/cameras-and-viewports.md

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The _Camera_ class and _Viewport_ class will seem very familiar if one has every
77

88
## Using a Camera
99

10-
The [Camera](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/graphics/Camera.kt) class is an abstract base that can be extended to create our own implementation of a camera. Luckily, we can use the [OrthographicCamera](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/graphics/Camera.kt#L339) implementation that is already offered.
10+
The [Camera](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/graphics/Camera.kt) class is an abstract base that can be extended to create our own implementation of a camera. Luckily, we can use the [OrthographicCamera](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/graphics/Camera.kt#L339) implementation that is already offered.
1111

1212
The camera allows us to:
1313

@@ -29,11 +29,10 @@ When using a camera we want to make sure we call the `update()` method on it bef
2929
```kotlin
3030
val camera = OrthographicCamera(graphics.width, graphics.height)
3131

32-
onRender {
33-
gl.clear(ClearBufferMask.COLOR_BUFFER_BIT)
34-
32+
onUpdate {
33+
val renderPass = ... // render pass releated setup
3534
camera.update()
36-
batch.use(camera.viewProjection) {
35+
batch.use(renderPass, camera.viewProjection) {
3736
// we are using the cameras view projection matrix to render
3837
}
3938
}
@@ -50,7 +49,7 @@ onResize { width, height ->
5049

5150
## Using a Viewport
5251

53-
Instead of managing the size of the camera ourselves, we can use a _Viewport_ instead to handle the sizing. When creating a new _Viewport_ we can either pass in our own _Camera_ instance or let the viewport create its own which we then can reference.
52+
Instead of managing the size of the camera ourselves, we can use a _Viewport_ instead to handle the sizing. When creating a new _Viewport_ we can either pass in our own _Camera_ instance or let the viewport create its own which we then can reference. WebGPU has certain limitations on viewports, when coming from OpenGL, may seem restrictive. For instance, viewports may not extend the size of the frame buffer it is drawing to. This makes things like a "FitViewport", where certain sides are extended outward, a bit harder to calculate as WebGPU will throw a fatal error when it detects it.
5453

5554
```kotlin
5655
val viewport = ExtendViewport(480, 270)
@@ -61,39 +60,20 @@ A viewport can be resized by using the `update()` method which will also update
6160

6261
```kotlin
6362
onResize { width, height ->
64-
viewport.update(width, height, context)
63+
viewport.update(width, height)
6564
}
6665
```
6766

68-
### Multiple Viewports
69-
70-
When we have multiple viewports, we must make sure we to apply the _Viewport_ before rendering so that the `glViewport` is set.
71-
72-
```kotlin
73-
viewport.apply(context)
74-
// draw using first viewport
75-
viewport2.apply(context)
76-
// draw using the second viewport
77-
```
78-
7967
## Types of Viewports
8068

8169
### Stretch Viewport
8270

83-
A viewport that supports using a virtual size (via [StretchViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/util/viewport/ScalingViewports.kt#L34)). The virtual viewport is strechted to fit the screen. There are no black bars and the aspect ratio can change after scaling.
84-
85-
### Fit Viewport
86-
87-
A viewport that supports using a virtual size (via [FitViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/util/viewport/ScalingViewports.kt#L29)). The virtual viewport will maintain its aspect ratio while attempting to fit as much as possible onto the screen. Black bars may appear.
88-
89-
### Fill Viewport
90-
91-
A viewport that supports using a virtual size (via [FillViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/util/viewport/ScalingViewports.kt#L39)). The virtual viewport will maintain its aspect ratio but in an attempt to fiill the screen parts of the viewport may be cut off. No black bars may appear.
71+
A viewport that supports using a virtual size (via [StretchViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/util/viewport/ScalingViewports.kt#L34)). The virtual viewport is strechted to fit the screen. There are no black bars and the aspect ratio can change after scaling.
9272

9373
### Extend Viewport
9474

95-
A viewport that supports using a virtual size (via [ExtendViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/util/viewport/ExtendViewport.kt)). The virtual viewport maintains the aspect ratio by extending the game world horizontally or vertically. The world is scaled to fit within the viewport and then the shorter dimension is lengthened to fill the viewport.
75+
A viewport that supports using a virtual size (via [ExtendViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/util/viewport/ExtendViewport.kt)). The virtual viewport maintains the aspect ratio by extending the game world horizontally or vertically. The world is scaled to fit within the viewport and then the shorter dimension is lengthened to fill the viewport.
9676

9777
### Screen Viewport
9878

99-
A viewport that uses a virtual size that will always match the window size (via [ScreenViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/lehaine/littlekt/util/viewport/ScreenViewport.kt)). No scaling happens along with no black bars appearing.
79+
A viewport that uses a virtual size that will always match the window size (via [ScreenViewport](https://github.com/littlektframework/littlekt/blob/master/core/src/commonMain/kotlin/com/littlekt/util/viewport/ScreenViewport.kt)). No scaling happens along with no black bars appearing.

_docs/2d/frame-buffer-objects.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)