Summerize Chapter 15: GPU-Driven Rendering
This commit is contained in:
parent
ccc878280d
commit
ace2b37857
|
|
@ -0,0 +1,78 @@
|
|||
# Chapter 15: GPU-Driven Rendering
|
||||
|
||||
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.
|
||||
|
||||
## The argument buffers
|
||||
|
||||
Many small buffers are created to set model information and copy into one large buffer. But we don't need any small buffers to upload model's information. We can set argument buffers for models directly to one large buffer. Also creating small buffers aren't the good practice.
|
||||
|
||||
In Renderer.swift, in initializeCommand(), change this:
|
||||
|
||||
```
|
||||
var mBuffers: [MTLBuffer] = []
|
||||
var mBuffersLength = 0
|
||||
for model in models {
|
||||
let encoder = icbComputeFunction.makeArgumentEncoder(bufferIndex: Int(BufferIndexModels.rawValue))
|
||||
let mBuffer = Renderer.device.makeBuffer(length: encoder.encodedLength, options: [])!
|
||||
encoder.setArgumentBuffer(mBuffer, offset: 0)
|
||||
encoder.setBuffer(model.vertexBuffer, offset: 0, index: 0)
|
||||
encoder.setBuffer(model.submesh.indexBuffer.buffer, offset: 0, index: 1)
|
||||
encoder.setBuffer(model.texturesBuffer, offset: 0, index: 2)
|
||||
encoder.setRenderPipelineState(model.pipelineState, index: 3)
|
||||
mBuffers.append(mBuffer)
|
||||
mBuffersLength += mBuffer.length
|
||||
}
|
||||
modelsBuffer = Renderer.device.makeBuffer(length: mBuffersLength, options: [])
|
||||
modelsBuffer.label = "Models Array Buffer"
|
||||
var offset = 0
|
||||
for mBuffer in mBuffers {
|
||||
var pointer = modelsBuffer.contents()
|
||||
pointer = pointer.advanced(by: offset)
|
||||
pointer.copyMemory(from: mBuffer.contents(), byteCount: mBuffer.length)
|
||||
offset += mBuffer.length
|
||||
}
|
||||
```
|
||||
|
||||
To:
|
||||
|
||||
```
|
||||
let encoder = icbComputeFunction.makeArgumentEncoder(bufferIndex: Int(BufferIndexModels.rawValue))
|
||||
let mBuffersLength = models.count * encoder.encodedLength
|
||||
modelsBuffer = Renderer.device.makeBuffer(length: mBuffersLength, options: [])
|
||||
modelsBuffer.label = "Models Array Buffer"
|
||||
var offset = 0
|
||||
for model in models {
|
||||
encoder.setArgumentBuffer(modelsBuffer, offset: offset)
|
||||
encoder.setBuffer(model.vertexBuffer, offset: 0, index: 0)
|
||||
encoder.setBuffer(model.submesh.indexBuffer.buffer, offset: 0, index: 1)
|
||||
encoder.setBuffer(model.texturesBuffer, offset: 0, index: 2)
|
||||
encoder.setRenderPipelineState(model.pipelineState, index: 3)
|
||||
offset += encoder.encodedLength
|
||||
}
|
||||
```
|
||||
|
||||
## Draw arguments
|
||||
|
||||
A MTLDrawIndexedPrimitivesIndirectArguments is created as local variable and sets values and copy to a buffer. Actually we don't need a local variable and can set values into a buffer directly.
|
||||
|
||||
In Renderer.swift, in initializeCommand(), change this in for loop:
|
||||
|
||||
```
|
||||
var drawArgument = MTLDrawIndexedPrimitivesIndirectArguments()
|
||||
drawArgument.indexCount = UInt32(model.submesh.indexCount)
|
||||
drawArgument.instanceCount = 1
|
||||
drawArgument.indexStart = UInt32(model.submesh.indexBuffer.offset)
|
||||
drawArgument.baseVertex = 0
|
||||
drawArgument.baseInstance = UInt32(modelIndex)
|
||||
drawPointer.pointee = drawArgument
|
||||
```
|
||||
|
||||
To:
|
||||
|
||||
```
|
||||
drawPointer.pointee.indexCount = UInt32(model.submesh.indexCount)
|
||||
drawPointer.pointee.instanceCount = 1
|
||||
drawPointer.pointee.indexStart = UInt32(model.submesh.indexBuffer.offset)
|
||||
drawPointer.pointee.baseVertex = 0
|
||||
drawPointer.pointee.baseInstance = UInt32(modelIndex)
|
||||
```
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
//
|
||||
/**
|
||||
* Copyright (c) 2019 Razeware LLC
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, create a derivative work, and/or sell copies of the
|
||||
* Software in any work that is designed, intended, or marketed for pedagogical or
|
||||
* instructional purposes related to programming, coding, application development,
|
||||
* or information technology. Permission for such use, copying, modification,
|
||||
* merger, publication, distribution, sublicensing, creation of derivative works,
|
||||
* or sale is expressly withheld.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#include <metal_stdlib>
|
||||
using namespace metal;
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
//
|
||||
/**
|
||||
* Copyright (c) 2019 Razeware LLC
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, create a derivative work, and/or sell copies of the
|
||||
* Software in any work that is designed, intended, or marketed for pedagogical or
|
||||
* instructional purposes related to programming, coding, application development,
|
||||
* or information technology. Permission for such use, copying, modification,
|
||||
* merger, publication, distribution, sublicensing, creation of derivative works,
|
||||
* or sale is expressly withheld.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
import Foundation
|
||||
|
|
@ -29,6 +29,7 @@ You can see color images in this book at [here](https://www.raywenderlich.com/bo
|
|||
* [Chapter 12: Environment](https://github.com/daemyung/metal-by-tutorials/tree/main/12-environment)
|
||||
* [Chapter 13: Instancing & Procedural Generation](https://github.com/daemyung/metal-by-tutorials/tree/main/13-procedural-generation)
|
||||
* [Chapter 14: Multipass & Deferred Rendering](https://github.com/daemyung/metal-by-tutorials/tree/main/14-multipass-rendering)
|
||||
* [Chapter 15: GPU-Driven Rendering](https://github.com/daemyung/metal-by-tutorials/tree/main/15-gpu-driven-rendering)
|
||||
|
||||
## Copyright
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue