2016-03-25 9 views
7

मैं वर्तमान में एक ग्राफिक खींचने की कोशिश कर रहा हूं जो स्विफ्ट में धातु का उपयोग करके एनिमेटेड होगा। मैंने अपने ग्राफिक के एक फ्रेम को सफलतापूर्वक खींचा है। ग्राफिक सरल है, जैसा कि आप इस छवि से देख सकते हैं। मैं यह नहीं समझ सकता कि ड्राइंग को मल्टी नमूना कैसे करें। सामान्य रूप से धातु पर बहुत कम संदर्भ प्रतीत होता है, खासकर स्विफ्ट वाक्यविन्यास के संबंध में।धातु में बहु-नमूना/जालीदार किनारों (आईओएस)

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 

सवाल है, मैं कैसे इन सिरों को सम करते हैं?

अद्यतन:

तो मैं एक MultiSample बनावट लागू किया और 4. करने के लिए sampleCount सेट मैं किसी भी अंतर नोटिस नहीं है इसलिए मुझे लगता है मैंने कुछ गलत किया है।

अंतिम:

तो अंत में यह दिखाई बहु नमूना काम करता है। शुरुआत में मेरे पास 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. जब एक प्रस्तुत करना पाइपलाइन राज्य बनाने, अपने multisample गिनती करने के लिए अपने प्रस्तुत करना पाइपलाइन राज्य डिस्क्रिप्टर का sampleCount निर्धारित किया है।

  2. स्टार्टअप पर

    , और जब भी परत का आकार बदलता है, एक बनावट वर्णनकर्ता जिसका textureTypeMTLTextureType2DMultisample और जिसका sampleCount अपने multisample गिनती है बनाकर अपने परत की drawable आकार के बराबर आयामों के साथ एक multisample बनावट पैदा करते हैं। यदि आप गहराई और/या स्टैंसिल बफर का उपयोग कर रहे हैं, तो इन गुणों को उनके वर्णनकर्ताओं पर भी सेट करें।

  3. जब प्रतिपादन, पास वर्णनकर्ता के प्रस्तुत करना प्राथमिक रंग लगाव की texture रूप MSAA बनावट निर्धारित करते हैं, और resolveTexture के रूप में वर्तमान drawable की बनावट निर्धारित किया है।

  4. MTLStoreActionMultisampleResolve पर रंग अनुलग्नक के storeAction सेट करें ताकि पास के अंत में एमएसएए बनावट को रेंडरबफर में हल किया जा सके।

  5. ड्रा और उपस्थित करें जैसा कि आप सामान्य रूप से करेंगे।

+0

ध्यान दें कि चरण 2 में, आपको एमएसएए बनावट के 'स्टोरेज मोड' को 'MTLStorageModePrivate' पर भी सेट करना होगा। आदर्श रूप में, आपको 'उपयोग' को' MTLTextureUsageRenderTarget' पर भी सेट करना चाहिए। – warrenm

संबंधित मुद्दे