2016-03-25 14 views
7

現在、SwiftのMetalを使用してアニメーション化されるグラフィックを描画しようとしています。私は自分のグラフィックの1つのフレームをうまく描いています。この画像からわかるように、グラフィックはシンプルです。私が理解できないことは、図面をマルチサンプルする方法です。一般的に、特にスウィフトの構文に関しては、メタルについてはほとんど参考にならないようです。金属のマルチサンプリング/ジグザグのエッジ(iOS)

self.metalLayer = CAMetalLayer() 
self.metalLayer.device = self.device 
self.metalLayer.pixelFormat = .BGRA8Unorm 
self.metalLayer.framebufferOnly = true 
self.metalLayer.frame = self.view.frame 
self.view.layer.addSublayer(self.metalLayer) 

self.renderer = SunRenderer(device: self.device, frame: self.view.frame) 

let defaultLibrary = self.device.newDefaultLibrary() 
let fragmentProgram = defaultLibrary!.newFunctionWithName("basic_fragment") 
let vertexProgram = defaultLibrary!.newFunctionWithName("basic_vertex") 

let pipelineStateDescriptor = MTLRenderPipelineDescriptor() 
pipelineStateDescriptor.vertexFunction = vertexProgram 
pipelineStateDescriptor.fragmentFunction = fragmentProgram 
pipelineStateDescriptor.colorAttachments[0].pixelFormat = .BGRA8Unorm 
pipelineStateDescriptor.colorAttachments[0].blendingEnabled = true 

pipelineStateDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperation.Add 
pipelineStateDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperation.Add 
pipelineStateDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.SourceAlpha 
pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha 
pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha 
pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha 

質問、どのように私はこれらのエッジを滑らかにしますか?

UPDATE:

だから私は、マルチサンプルの質感を実現し、私は私が何か間違ったことをした疑いがあるので、私は何の違いに気づくことはありません。4.にsampleCountを設定しています。

FINAL:

だから最後にそれはマルチサンプリング作品を表示されません。最初は、これらの "光線"を0アルファでラッピングする頂点を持っていました。これは滑らかなエッジを作るためのトリックです。これらの頂点では、マルチサンプリングはエッジを改善するようには見えませんでした。私がレイに4つの頂点を持つように戻ったとき、マルチサンプリングはエッジを改善しました。

let defaultLibrary = self.device.newDefaultLibrary() 
let fragmentProgram = defaultLibrary!.newFunctionWithName("basic_fragment") 
let vertexProgram = defaultLibrary!.newFunctionWithName("basic_vertex") 

let pipelineStateDescriptor = MTLRenderPipelineDescriptor() 
pipelineStateDescriptor.vertexFunction = vertexProgram 
pipelineStateDescriptor.fragmentFunction = fragmentProgram 
pipelineStateDescriptor.colorAttachments[0].pixelFormat = .BGRA8Unorm 
pipelineStateDescriptor.colorAttachments[0].blendingEnabled = true 
pipelineStateDescriptor.sampleCount = 4 

pipelineStateDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperation.Add 
pipelineStateDescriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperation.Add 
pipelineStateDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.SourceAlpha 
pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.SourceAlpha 

pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha 
pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha 

let desc = MTLTextureDescriptor() 
desc.textureType = MTLTextureType.Type2DMultisample 
desc.width = Int(self.view.frame.width) 
desc.height = Int(self.view.frame.height) 
desc.sampleCount = 4 
desc.pixelFormat = .BGRA8Unorm 

self.sampletex = self.device.newTextureWithDescriptor(desc) 


// When rendering 
let renderPassDescriptor = MTLRenderPassDescriptor() 
renderPassDescriptor.colorAttachments[0].texture = sampletex 
renderPassDescriptor.colorAttachments[0].resolveTexture = drawable.texture 
renderPassDescriptor.colorAttachments[0].loadAction = .Clear 
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 23/255.0, green: 26/255.0, blue: 31/255.0, alpha: 0.0) 
renderPassDescriptor.colorAttachments[0].storeAction = .MultisampleResolve 


let commandBuffer = commandQueue.commandBuffer() 

let renderEncoder = commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor) 
renderEncoder.setRenderPipelineState(pipelineState) 

答えて

8

これはMTKViewと実質的に単純である(ちょうどあなたの希望ビューのMSAAサンプルの数とパイプラインの記述子にsampleCountを設定)、しかしここにあなた自身の圧延のための手順です。

  1. レンダリングパイプライン状態を作り出す

    は、お使いのマルチサンプル数にあなたのレンダリングパイプライン状態記述子のsampleCountを設定します。起動時

  2. 、および層はサイズを変更したときに、そのtextureTypeMTLTextureType2DMultisamplesampleCountあなたのマルチサンプル数である質感記述子を作成してレイヤーの描画可能なサイズと同じ大きさでマルチサンプルのテクスチャを作成します。深度バッファまたはステンシルバッファを使用している場合は、これらのプロパティも記述子に設定します。

  3. レンダリング時に、レンダーパスディスクリプタのプライマリカラーアタッチメントのtextureとしてMSAAテクスチャを設定し、現在の描画可能テクスチャをresolveTextureとして設定します。

  4. カラーアタッチメントのstoreActionMTLStoreActionMultisampleResolveに設定して、パスの最後にMSAAテクスチャがレンダバッファに解決されるようにします。

  5. 通常どおりに描画してください。

+0

ステップ2では、MSAAテクスチャの 'storageMode'も' MTLStorageModePrivate'に設定する必要があることに注意してください。理想的には、 'usage'を' MTLTextureUsageRenderTarget'に設定するべきです。 – warrenm

関連する問題