Summerize Chapter 1: Hello, Metal
This commit is contained in:
parent
d0896dd159
commit
dd39d514b5
|
|
@ -0,0 +1,17 @@
|
|||
# Chapter 1: Hello, Metal!
|
||||
|
||||
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 pipeline state
|
||||
|
||||
When this book creates a pipeline state, they manually set the output pixel format like below.
|
||||
|
||||
```
|
||||
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
|
||||
```
|
||||
|
||||
This is fine but the output pixel format of view isn't always `.bgra8Unorm`. It can be `.rgba8Unorm` or other formats. Thus we need to get this information from a view like below.
|
||||
|
||||
```
|
||||
pipelineDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat;
|
||||
```
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* 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 PlaygroundSupport
|
||||
import MetalKit
|
||||
|
||||
guard let device = MTLCreateSystemDefaultDevice() else {
|
||||
fatalError("GPU is not supported")
|
||||
}
|
||||
|
||||
let frame = CGRect(x: 0, y: 0, width: 600, height: 600)
|
||||
let view = MTKView(frame: frame, device: device)
|
||||
view.clearColor = MTLClearColor(red: 1, green: 1, blue: 0.8, alpha: 1)
|
||||
|
||||
let allocator = MTKMeshBufferAllocator(device: device)
|
||||
let mdlMesh = MDLMesh(sphereWithExtent: [0.2, 0.75, 0.2],
|
||||
segments: [100, 100],
|
||||
inwardNormals: false,
|
||||
geometryType: .triangles,
|
||||
allocator: allocator)
|
||||
let mesh = try MTKMesh(mesh: mdlMesh, device: device)
|
||||
|
||||
guard let commandQueue = device.makeCommandQueue() else {
|
||||
fatalError("Could not create a command queue")
|
||||
}
|
||||
|
||||
let shader = """
|
||||
#include <metal_stdlib>
|
||||
using namespace metal;
|
||||
struct VertexIn {
|
||||
float4 position [[ attribute(0) ]];
|
||||
};
|
||||
vertex float4 vertex_main(const VertexIn vertex_in [[ stage_in ]]) {
|
||||
return vertex_in.position;
|
||||
}
|
||||
fragment float4 fragment_main() {
|
||||
return float4(0, 0.4, 0.21, 1);
|
||||
}
|
||||
"""
|
||||
|
||||
let library = try device.makeLibrary(source: shader, options: nil)
|
||||
let vertexFunction = library.makeFunction(name: "vertex_main")
|
||||
let fragmentFunction = library.makeFunction(name: "fragment_main")
|
||||
|
||||
let pipelineDescriptor = MTLRenderPipelineDescriptor()
|
||||
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
|
||||
pipelineDescriptor.vertexFunction = vertexFunction
|
||||
pipelineDescriptor.fragmentFunction = fragmentFunction
|
||||
|
||||
pipelineDescriptor.vertexDescriptor =
|
||||
MTKMetalVertexDescriptorFromModelIO(mesh.vertexDescriptor)
|
||||
|
||||
let pipelineState =
|
||||
try device.makeRenderPipelineState(descriptor: pipelineDescriptor)
|
||||
|
||||
guard let commandBuffer = commandQueue.makeCommandBuffer(),
|
||||
let renderPassDescriptor = view.currentRenderPassDescriptor,
|
||||
let renderEncoder =
|
||||
commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor)
|
||||
else { fatalError() }
|
||||
|
||||
renderEncoder.setRenderPipelineState(pipelineState)
|
||||
|
||||
renderEncoder.setVertexBuffer(mesh.vertexBuffers[0].buffer,
|
||||
offset: 0, index: 0)
|
||||
|
||||
guard let submesh = mesh.submeshes.first else {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
renderEncoder.drawIndexedPrimitives(type: .triangle,
|
||||
indexCount: submesh.indexCount,
|
||||
indexType: submesh.indexType,
|
||||
indexBuffer: submesh.indexBuffer.buffer,
|
||||
indexBufferOffset: 0)
|
||||
renderEncoder.endEncoding()
|
||||
guard let drawable = view.currentDrawable else {
|
||||
fatalError()
|
||||
}
|
||||
commandBuffer.present(drawable)
|
||||
commandBuffer.commit()
|
||||
|
||||
PlaygroundPage.current.liveView = view
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='macos'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* 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 PlaygroundSupport
|
||||
import MetalKit
|
||||
|
||||
guard let device = MTLCreateSystemDefaultDevice() else {
|
||||
fatalError("GPU is not supported")
|
||||
}
|
||||
|
||||
let frame = CGRect(x: 0, y: 0, width: 600, height: 600)
|
||||
let view = MTKView(frame: frame, device: device)
|
||||
view.clearColor = MTLClearColor(red: 1, green: 1, blue: 0.8, alpha: 1)
|
||||
|
||||
let allocator = MTKMeshBufferAllocator(device: device)
|
||||
let mdlMesh = MDLMesh(sphereWithExtent: [0.2, 0.75, 0.2],
|
||||
segments: [100, 100],
|
||||
inwardNormals: false,
|
||||
geometryType: .triangles,
|
||||
allocator: allocator)
|
||||
let mesh = try MTKMesh(mesh: mdlMesh, device: device)
|
||||
|
||||
guard let commandQueue = device.makeCommandQueue() else {
|
||||
fatalError("Could not create a command queue")
|
||||
}
|
||||
|
||||
let shader = """
|
||||
#include <metal_stdlib>
|
||||
using namespace metal;
|
||||
struct VertexIn {
|
||||
float4 position [[ attribute(0) ]];
|
||||
};
|
||||
vertex float4 vertex_main(const VertexIn vertex_in [[ stage_in ]]) {
|
||||
return vertex_in.position;
|
||||
}
|
||||
fragment float4 fragment_main() {
|
||||
return float4(0, 0.4, 0.21, 1);
|
||||
}
|
||||
"""
|
||||
|
||||
let library = try device.makeLibrary(source: shader, options: nil)
|
||||
let vertexFunction = library.makeFunction(name: "vertex_main")
|
||||
let fragmentFunction = library.makeFunction(name: "fragment_main")
|
||||
|
||||
let pipelineDescriptor = MTLRenderPipelineDescriptor()
|
||||
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
|
||||
pipelineDescriptor.vertexFunction = vertexFunction
|
||||
pipelineDescriptor.fragmentFunction = fragmentFunction
|
||||
|
||||
pipelineDescriptor.vertexDescriptor =
|
||||
MTKMetalVertexDescriptorFromModelIO(mesh.vertexDescriptor)
|
||||
|
||||
let pipelineState =
|
||||
try device.makeRenderPipelineState(descriptor: pipelineDescriptor)
|
||||
|
||||
guard let commandBuffer = commandQueue.makeCommandBuffer(),
|
||||
let renderPassDescriptor = view.currentRenderPassDescriptor,
|
||||
let renderEncoder =
|
||||
commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor)
|
||||
else { fatalError() }
|
||||
|
||||
renderEncoder.setRenderPipelineState(pipelineState)
|
||||
|
||||
renderEncoder.setVertexBuffer(mesh.vertexBuffers[0].buffer,
|
||||
offset: 0, index: 0)
|
||||
|
||||
guard let submesh = mesh.submeshes.first else {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
renderEncoder.drawIndexedPrimitives(type: .triangle,
|
||||
indexCount: submesh.indexCount,
|
||||
indexType: submesh.indexType,
|
||||
indexBuffer: submesh.indexBuffer.buffer,
|
||||
indexBufferOffset: 0)
|
||||
renderEncoder.endEncoding()
|
||||
guard let drawable = view.currentDrawable else {
|
||||
fatalError()
|
||||
}
|
||||
commandBuffer.present(drawable)
|
||||
commandBuffer.commit()
|
||||
|
||||
PlaygroundPage.current.liveView = view
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='macos'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
Loading…
Reference in New Issue