From 57a75d77c0aa8fa3a32a65362bc50fa88e78daf6 Mon Sep 17 00:00:00 2001 From: daemyung jang Date: Sat, 16 Apr 2022 00:01:05 +0900 Subject: [PATCH] Summerize Chapter 24: Performance Optimization --- .../README.md | 59 +++++++++++++++++++ README.md | 1 + 2 files changed, 60 insertions(+) create mode 100644 24-synchronization-and-multithreading/README.md diff --git a/24-synchronization-and-multithreading/README.md b/24-synchronization-and-multithreading/README.md new file mode 100644 index 0000000..c64e104 --- /dev/null +++ b/24-synchronization-and-multithreading/README.md @@ -0,0 +1,59 @@ +# 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]]) +``` diff --git a/README.md b/README.md index 5899d10..1837515 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ You can see color images in this book at [here](https://www.raywenderlich.com/bo * [Chapter 21: Metal Performance Shaders](https://github.com/daemyung/metal-by-tutorials/tree/main/21-metal-performance-shaders) * [Chapter 22: Integrating SpirteKit & SceneKit](https://github.com/daemyung/metal-by-tutorials/tree/main/22-integrating-spritekit-scenekit) * [Chapter 23: Debugging & Profiling](https://github.com/daemyung/metal-by-tutorials/tree/main/23-debugging-and-profiling) + * [Chapter 24: Performance Optimization](https://github.com/daemyung/metal-by-tutorials/tree/main/24-synchronization-and-multithreading) ## Copyright