MetalByTutorials/24-synchronization-and-mult...
daemyung jang 57a75d77c0
Summerize Chapter 24: Performance Optimization
2022-04-16 00:01:05 +09:00
..
final/Debugging Copy source code from the official website 2021-04-15 11:39:00 +09:00
starter/Debugging Copy source code from the official website 2021-04-15 11:39:00 +09:00
README.md Summerize Chapter 24: Performance Optimization 2022-04-16 00:01:05 +09:00
references.markdown Copy source code from the official website 2021-04-15 11:39:00 +09:00

README.md

Chapter 24: Performance Optimization

As I read this book, I summarize what I think is wrong. If you think my comments are wrong then please let me know. We can dicuss more and update your opinion.

CPU-GPU synchronization

For the triple buffering, currentUniformIndex is used to point a current uniform data. However this logic is totally wrong because uniform data which is sent to GPU is invalid. Let's print uniform data and see what's wrong.

In Scene.swift, in update(deltaTime:), add this code before currentUniformIndex is updated:

print("Update: \(uniforms[currentUniformIndex].viewMatrix)")

In Renderer.swift, in draw(in view:), add this line after declaring uniforms:

print("Draw: \(uniforms.viewMatrix)")

Build and run. You will see logs like below:

Update: simd_float4x4([[0.99999994, 0.0, 0.0, 0.0], [0.0, 0.99999994, 0.0, 0.0], [0.0, 0.0, 0.99999994, 0.0], [0.0, -0.99999994, 0.99999994, 0.99999994]])
Draw: simd_float4x4([[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]])

We've updated uniform data in update(deltaTime:) but sent uniform data to GPU is the garbage. To fix this, we need to keep the uniform index for rendering.

In Scene.swift, add this property:

var renderUniformIndex = 0

In Scene.swift, in update(deltaTime:), add this code before currentUniformIndex is updated:

renderUniformIndex = currentUniformIndex

In Renderer.swift, in draw(in view:), replace this code:

let uniforms = scene.uniforms[scene.currentUniformIndex]

With:

let uniforms = scene.uniforms[scene.renderUniformIndex]

Build and run. Now we send valid uniform data to GPU:

Update: simd_float4x4([[0.99999994, 0.0, 0.0, 0.0], [0.0, 0.99999994, 0.0, 0.0], [0.0, 0.0, 0.99999994, 0.0], [0.0, -0.99999994, 0.99999994, 0.99999994]])
Draw: simd_float4x4([[0.99999994, 0.0, 0.0, 0.0], [0.0, 0.99999994, 0.0, 0.0], [0.0, 0.0, 0.99999994, 0.0], [0.0, -0.99999994, 0.99999994, 0.99999994]])